package Demo01Tcp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) throws IOException {
Socket socket=new Socket("127.0.0.1",1234);
OutputStream os=socket.getOutputStream();
os.write("你好,服务器".getBytes());
InputStream is=socket.getInputStream();
byte[] bytes=new byte[1024];
int len=is.read(bytes);
System.out.println(new String(bytes,0,len));
}
}
package Demo01Tcp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(1234);
Socket socket=server.accept();
InputStream is=socket.getInputStream();
byte[] bytes =new byte[1024];
int len =is.read(bytes);
System.out.println(new String(bytes,0,len));
OutputStream os=socket.getOutputStream();
os.write("收到数据,谢谢".getBytes());
socket.close();
socket.close();
}
}