1.UDP的发生和接收
发送:
public class udpSend {
// 创建DatagramSocket, 随机端口号
// 创建DatagramPacket, 指定数据, 长度, 地址, 端口
// 使用DatagramSocket发送DatagramPacket
// 关闭DatagramSocket
public static void main(String[] args) throws IOException {
String meg = "dumn";
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(meg.getBytes(), meg.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);
socket.send(packet);
socket.close();
}
}
接收:
public class udpReceive {
// 创建DatagramSocket, 指定端口号
// 创建DatagramPacket, 指定数组, 长度
// 使用DatagramSocket接收DatagramPacket
// 关闭DatagramSocket
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(6666);
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
多线程版本:
public class ReceiveSendThread {
public static void main(String[] args) {
new receive().start();
new send().start();
}
}
class receive extends Thread{
@Override
public void run() {
try {
DatagramSocket socket = new DatagramSocket(6666);
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
socket.receive(packet);
String ip = packet.getAddress().getHostAddress();
System.out.println("ip"+ip+" :"+new String(packet.getData(),0,packet.getLength()));
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class send extends Thread{
@Override
public void run() {
try {
String meg = "dumn";
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(meg.getBytes(), meg.getBytes().length, InetAddress.getByName("127.0.0.1"), 6666);
socket.send(packet);
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.TCP的发送和接收
客户端:
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1", 6666);
// InputStream is = socket.getInputStream();
// OutputStream os = socket.getOutputStream();
// byte[] arr = new byte[1024];
// int len = is.read(arr);
// System.out.println(new String(arr,0,len));
// os.write("挖掘机哪家强".getBytes());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
System.out.println(br.readLine());
ps.println("问你下");
System.out.println(br.readLine());
socket.close();
}
服务端:
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(6666);
Socket socket = server.accept();
// InputStream is = socket.getInputStream();
// OutputStream os = socket.getOutputStream();
// os.write("百度一下试试".getBytes());
// byte[] arr = new byte[1024];
// int len = is.read(arr);
// System.out.println(new String(arr,0,len));
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("百度一下啊");
System.out.println(br.readLine());
ps.println("自己去百度");
socket.close();
}
网友评论