java socket
用socket通讯写出客户端和服务器端的通讯,要求客户发送数据后能够回显相同的数据
public class ServerSocket_1
{
public static void main(String[] args)
throws Exception
{
ServerSocket ss = new ServerSocket(4001);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.
getInputStream()));
PrintStream ps=new PrintStream(s.getOutputStream());
String temp = br.readLine();
while (true)
{
System.out.println("客户端:"+temp);
ps.println(temp);
if (temp.equals("stop"))
{
break;
}
temp = br.readLine();
}
br.close();
ps.close();
ss.close();
}
}
public class ClientSocket
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("localhost", 4001);
PrintStream ps = new PrintStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br_server = new BufferedReader(new InputStreamReader(s.
getInputStream()));
String temp = br.readLine();
while (true)
{
ps.println(temp);
temp = br_server.readLine();
System.out.println("服务器的信息:" + temp);
if (temp.equals("stop"))
{
break;
}
temp = br.readLine();
}
s.close();
br.close();
br_server.close();
}
}
public class ServerSocket_1
{
public static void main(String[] args)
throws Exception
{
ServerSocket ss = new ServerSocket(4001);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.
getInputStream()));
PrintStream ps=new PrintStream(s.getOutputStream());
String temp = br.readLine();
while (true)
{
System.out.println("客户端:"+temp);
ps.println(temp);
if (temp.equals("stop"))
{
break;
}
temp = br.readLine();
}
br.close();
ps.close();
ss.close();
}
}
public class ClientSocket
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("localhost", 4001);
PrintStream ps = new PrintStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br_server = new BufferedReader(new InputStreamReader(s.
getInputStream()));
String temp = br.readLine();
while (true)
{
ps.println(temp);
temp = br_server.readLine();
System.out.println("服务器的信息:" + temp);
if (temp.equals("stop"))
{
break;
}
temp = br.readLine();
}
s.close();
br.close();
br_server.close();
}
}