基於TCP的socket實現
SocketClient.java
public class SocketClient { public static void main(String[] args) throws InterruptedException { try { // 和服務器創建連接 Socket socket = new Socket("localhost",8088); // 要發送給服務器的信息 OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); pw.write("客戶端發送信息"); pw.flush(); socket.shutdownOutput(); // 從服務器接收的信息 InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String info = null; while((info = br.readLine())!=null){ System.out.println("我是客戶端,服務器返回信息:"+info); } br.close(); is.close(); os.close(); pw.close(); socket.close(); } catch (Exception e) { e.printStackTrace(); } } } 原文鏈接:https://blog.csdn.net/u014209205/article/details/80461122
SocketServer.java
public class SocketServer { public static void main(String[] args) { try { // 創建服務端socket ServerSocket serverSocket = new ServerSocket(8088); // 創建客戶端socket Socket socket = new Socket(); //循環監聽等待客戶端的連接 while(true){ // 監聽客戶端 socket = serverSocket.accept(); ServerThread thread = new ServerThread(socket); thread.start(); InetAddress address=socket.getInetAddress(); System.out.println("當前客戶端的IP:"+address.getHostAddress()); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } } 原文鏈接:https://blog.csdn.net/u014209205/article/details/80461122
ServerThread.java
public class ServerThread extends Thread{ private Socket socket = null; public ServerThread(Socket socket) { this.socket = socket; } @Override public void run() { InputStream is=null; InputStreamReader isr=null; BufferedReader br=null; OutputStream os=null; PrintWriter pw=null; try { is = socket.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String info = null; while((info=br.readLine())!=null){ System.out.println("我是服務器,客戶端說:"+info); } socket.shutdownInput(); os = socket.getOutputStream(); pw = new PrintWriter(os); pw.write("服務器歡迎你"); pw.flush(); } catch (Exception e) { // TODO: handle exception } finally{ //關閉資源 try { if(pw!=null) pw.close(); if(os!=null) os.close(); if(br!=null) br.close(); if(isr!=null) isr.close(); if(is!=null) is.close(); if(socket!=null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } ———————————————— 原文鏈接:https://blog.csdn.net/u014209205/article/details/80461122
基於UDP的socket實現
SocketClient.java
public class SocketClient {
public static void main(String[] args) {
try {
// 要發送的消息
String sendMsg = "客戶端發送的消息";
// 獲取服務器的地址
InetAddress addr = InetAddress.getByName("localhost");
// 創建packet包對象,封裝要發送的包數據和服務器地址和端口號
DatagramPacket packet = new DatagramPacket(sendMsg.getBytes(),
sendMsg.getBytes().length, addr, 8088);
// 創建Socket對象
DatagramSocket socket = new DatagramSocket();
// 發送消息到服務器
socket.send(packet);
// 關閉socket
socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
————————————————
原文鏈接:https://blog.csdn.net/u014209205/article/details/80461122
SocketServer.java
1 public class SocketServer { 2 3 public static void main(String[] args) { 4 try { 5 // 要接收的報文 6 byte[] bytes = new byte[1024]; 7 DatagramPacket packet = new DatagramPacket(bytes, bytes.length); 8 9 // 創建socket並指定端口 10 DatagramSocket socket = new DatagramSocket(8088); 11 12 // 接收socket客戶端發送的數據。如果未收到會一致阻塞 13 socket.receive(packet); 14 String receiveMsg = new String(packet.getData(),0,packet.getLength()); 15 System.out.println(packet.getLength()); 16 System.out.println(receiveMsg); 17 18 // 關閉socket 19 socket.close(); 20 } catch (Exception e) { 21 // TODO: handle exception 22 e.printStackTrace(); 23 } 24 } 25 26 } 27 ———————————————— 28 原文鏈接:https://blog.csdn.net/u014209205/article/details/80461122
