在eclipse中。写两个class文件,一个客户端一个服务端,为什么两个相互传消息不能全部显示呢?
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- //演示tcp的传输,客户端和服务端互访
- public class TcpClient1 {
- public static void main(String[] args) throws Exception {
- //客户端发出信息
- Socket s = new Socket("192.168.0.5",6666);
- OutputStream out = s.getOutputStream();
- out.write("客户端发出".getBytes());
- //接收服务端给的回馈信息
- InputStream in = s.getInputStream();
- byte[] buf = new byte[1024];
- int len = in.read(buf);
- System.out.println(new String(buf,0,len));
-
- s.close();
- }
- }
复制代码
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class TcpServer1 {
- public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(6666);
- //拿到客户端对象
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip);
-
- InputStream in = s.getInputStream();
- byte[] buf = new byte[1024];
- int len = in.read(buf);
- System.out.println(new String(buf,0,len));
-
- OutputStream out = s.getOutputStream();
- out.write("服务端收到".getBytes());
- s.close();
- ss.close();
- }
- }
复制代码
|
|