Java网络编程TCP/IP,UDP
程序员文章站
2022-06-30 10:19:04
...
TCP/IP
客户端
package com.cxx.netty;
import java.io.*;
import java.net.Socket;
/**
* @Author: cxx
* 客户端
* @Date: 2018/5/27 14:14
*/
public class GreetingClient {
private static String host="127.0.0.1";
private static int port = 9999;
public static void main(String[] args) throws InterruptedException {
for (int i=0;i<100;i++){
start(i);
}
}
public static void start(int i){
try{
Socket client = new Socket(host,port);
print(client);
OutputStream outputStream = client.getOutputStream();
DataOutput dout = new DataOutputStream(outputStream);
dout.writeUTF(i+":hello server");
InputStream inputStream = client.getInputStream();
DataInput din = new DataInputStream(inputStream);
System.out.println("服务器响应:"+din.readUTF());
System.out.println();
}catch (Exception e){
e.printStackTrace();
}
}
public static void print(Socket client){
System.out.println("远程主机ip:"+client.getRemoteSocketAddress());
}
}
服务端
package com.cxx.netty;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Author: cxx
* 服务端
* @Date: 2018/5/27 14:17
*/
public class GreetingServer extends Thread {
private static int port=9999;
private static boolean flag=true;
public static void main(String[] args){
Thread t = new GreetingServer();
t.run();
}
@Override
public void run() {
try {
System.out.println("开启服务端..等待连接:");
ServerSocket client = new ServerSocket(port);
// client.setSoTimeout(10000);
while (flag){
Socket server = client.accept();
DataInput din = new DataInputStream(server.getInputStream());
System.out.println("server:"+din.readUTF());
DataOutput dou = new DataOutputStream(server.getOutputStream());
dou.writeUTF("谢谢你连接我!");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
运行结果
UDP构建一个简单的聊天室
package com.cxx.netty;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/**
* @Author: cxx
* UDP实现一个聊天程序
* @Date: 2018/5/27 15:14
*/
public class UdpChat {
public static void main(String[] args) throws Exception {
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receiveSocket = new DatagramSocket(44444);
new Thread(new Send(sendSocket)).start();
new Thread(new Receive(receiveSocket)).start();
}
}
//定义发送线程任务
class Send implements Runnable{
private DatagramSocket socket;
public Send(DatagramSocket socket){
this.socket = socket;
}
@Override
public void run() {
//一直发送数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
try{
System.out.println("聊天室开启:");
while ((line=br.readLine())!=null){
byte[] data = line.getBytes();
DatagramPacket packet = new DatagramPacket(data,data.length, InetAddress.getByName("127.0.0.1"), 44444);
socket.send(packet);
if (line.equals("baibai")){
break;
}
}
br.close();
socket.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
//定义接收线程任务
class Receive implements Runnable{
private DatagramSocket socket;
public Receive(DatagramSocket socket){
this.socket=socket;
}
@Override
public void run() {
//一直接收数据
while (true) {
try {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
socket.receive(packet);
String line = new String(packet.getData(), 0, packet.getLength());
String ip = packet.getAddress().getHostAddress();
if (line.equals("baibai")) {
System.out.println(ip + "==离开了聊天室!");
}
System.out.println(ip + "说:" + line);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}