TCP
三次握手,四次挥手
InetAddress可以查看本机ip
发送端:
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(address, port);
OutputStream os = socket.getOutputStream();
os.write("测试数据".getBytes());
os.close();
socket.close();
接收端:
ServerSocket serverSocket = new ServerSocket(9999);
Socket accept = serverSocket.accept();
InputStream is = accept.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = is.read(bytes))!=-1){
byteArrayOutputStream.write(bytes,0,len);
}
System.out.println(byteArrayOutputStream);
UDP
发送端:
DatagramSocket datagramSocket = new DatagramSocket();
String msg = "你好啊";
InetAddress address = InetAddress.getByName("localhost");
int port = 6666;
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,address,port);
datagramSocket.send(datagramPacket);
datagramSocket.close();
接收端:
DatagramSocket datagramSocket = new DatagramSocket(6666);
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
datagramSocket.receive(datagramPacket);
System.out.println(datagramPacket.getAddress()+" : "+datagramPacket.getPort());
String s = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
System.out.println(s);
datagramSocket.close();
UPD聊天
send
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class Send implements Runnable {
private int fromPort;
private String toIP;
private int toPort;
DatagramSocket datagramSocket = null;
BufferedReader reader = null;
public Send(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
datagramSocket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
String s = reader.readLine();
DatagramPacket datagramPacket = new DatagramPacket(
s.getBytes(), 0, s.getBytes().length,
new InetSocketAddress(this.toIP, this.toPort)
);
datagramSocket.send(datagramPacket);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
datagramSocket.close();
}
}
}
receive
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class Receive implements Runnable{
DatagramSocket datagramSocket = null;
private int port;
private String name;
public Receive(int port,String name) {
this.port = port;
this.name = name;
try {
datagramSocket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true){
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
datagramSocket.receive(datagramPacket);
byte[] data = datagramPacket.getData();
System.out.println(datagramPacket.getAddress()+" : "+datagramPacket.getPort()+" ---> "+
name+" : "+new String(data,0,datagramPacket.getLength()));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
datagramSocket.close();
}
}
}
Student用户
public class Student {
public static void main(String[] args) {
new Thread(new Send(7777,"localhost",9999)).start();
new Thread(new Receive(8888,"老师")).start();
}
}
Teacher用户
public class Teacher {
public static void main(String[] args) {
new Thread(new Send(10001,"localhost",8888)).start();
new Thread(new Receive(9999,"同学")).start();
}
}
|