//TCP和UDP区别:tcp建立连接后通过流来进行通讯,udp直接往外面扔数据包
class TcpSenter{
public static void main(String[] agrs){
Socket clientSocket=new Socket("192.168.1.11",7789);
OutputStream os=clientSocket.getOutputStream();
String input="input";
byte[] inputArray=input.getBytes();
os.write(inputArray);//向服务端发送信息
InputStream is=clientSocket.getInputStream();
byte [] buffer=new byte[1024];
int num=is.read(buffer);
String result=new String(buffer,0,num);
}
}
class TcpReceiver{
Socket serverSocket=new Socket(7799);
Socket socket=serverSocket.accept();
InputStream is=scoket.getInputStream();
byte[] buffer=new byte[1024];
int num=is.read(buffer);
String temp=new String(buffer,0,number);//读数据
OutputStream os=socket.getOutputStream();
os.write("data".getBytes());
}
网友评论