UDP server
1 import java.io.IOException; 2 import java.net.DatagramPacket; 3 import java.net.DatagramSocket; 4 import java.net.InetSocketAddress; 5 import java.net.SocketException; 6 7 /** 8 * udpserver 9 * 监听端口 10001 10 * @author GXF 11 * 12 */ 13 public class UdpSocketServer { 14 private DatagramSocket serverSocket; 15 16 public UdpSocketServer(int port){ 17 init(port); 18 } 19 20 public void init(int port){ 21 22 InetSocketAddress serverSocketAddress = new InetSocketAddress( port); 23 try { 24 serverSocket = new DatagramSocket(serverSocketAddress); 25 } catch (SocketException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } 29 } 30 31 //监听端口 32 public void startListen(){ 33 byte buff[] = new byte[1024]; 34 DatagramPacket receivePacket = new DatagramPacket(buff, buff.length); 35 while(true){ 36 try { 37 serverSocket.receive(receivePacket); 38 String message = new String(buff, 0, receivePacket.getLength()); 39 System.out.println("message from client = " + message); 40 } catch (IOException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 } 45 } 46 47 public static void main(String args[]){ 48 UdpSocketServer udpSocketServer = new UdpSocketServer(10001); 49 udpSocketServer.startListen(); 50 } 51 }
UDP client
1 import java.io.IOException; 2 import java.net.DatagramPacket; 3 import java.net.DatagramSocket; 4 import java.net.InetAddress; 5 import java.net.InetSocketAddress; 6 import java.net.SocketException; 7 import java.net.UnknownHostException; 8 9 /** 10 * Udp客户端 DatagramSocket 11 * @author GXF 12 * 13 */ 14 public class UdpSocketClient { 15 //客户端套接字 16 private DatagramSocket clientSocket; 17 18 //构造方法 19 public UdpSocketClient(String ip, int port){ 20 init(ip, port); 21 } 22 23 /** 24 * 初始化套接字 25 * @param ip 26 * @param port 27 */ 28 public void init(String ip, int port){ 29 // try { 30 // InetAddress serverAddress = InetAddress.getByName(ip); 31 // InetSocketAddress serverSocketAddress = new InetSocketAddress(serverAddress, port); 32 try { 33 clientSocket = new DatagramSocket(); 34 } catch (SocketException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 // } catch (UnknownHostException | SocketException e) { 39 // e.printStackTrace(); 40 // } 41 } 42 43 /** 44 * 使用UDP向服务器发送消息 45 * @param ip 46 * @param message 47 */ 48 public void sendMessage(String message){ 49 byte buff[] = message.getBytes(); 50 DatagramPacket packetToSend; 51 try { 52 packetToSend = new DatagramPacket(buff, buff.length, InetAddress.getByName("127.0.0.1"), 10002); 53 clientSocket.send(packetToSend); //发送数据包 54 } catch (Exception e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 57 } 58 } 59 60 public static void main(String[] args) { 61 String serverIp = "127.0.0.1"; 62 int port = 10001; 63 UdpSocketClient clientSocket = new UdpSocketClient(serverIp, port); 64 String message = "hahahahah"; 65 clientSocket.sendMessage(message); 66 67 } 68 69 }
TCP server
import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class TcpServer { private ServerSocket serverSocket; TcpServer(int port){ try { serverSocket = new ServerSocket(port); } catch (IOException e) { e.printStackTrace(); } } //开始监听端口 public void start(){ while(true){ try { Socket clientSocket = serverSocket.accept(); InputStream is = clientSocket.getInputStream(); byte buff[] = new byte[1024]; int length = -1; StringBuilder sb = new StringBuilder(); // while((length = is.read(buff)) > 0){ length = is.read(buff); sb.append(new String(buff, 0, length)); // }//while System.out.println("message received: " + sb.toString()); clientSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String args[]){ int port = 10002; TcpServer tcpServer = new TcpServer(port); tcpServer.start(); } }
Tcp client
1 import java.io.IOException; 2 import java.io.OutputStream; 3 import java.net.InetAddress; 4 import java.net.Socket; 5 import java.util.Scanner; 6 7 public class TcpClient { 8 private Socket socket; 9 private int port; 10 11 private TcpClient(int port){ 12 // try { 13 this.port = port; 14 // } catch (IOException e) { 15 // e.printStackTrace(); 16 // } 17 } 18 19 /** 20 * 发送消息 21 * @param message 22 */ 23 public void sendMessage(String message){ 24 try { 25 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), port); 26 OutputStream os = socket.getOutputStream(); 27 byte buff[] = message.getBytes(); 28 os.write(buff); 29 os.flush(); 30 socket.close(); 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } 34 35 } 36 37 public void inputMessage(){ 38 Scanner scanner = new Scanner(System.in); 39 while(true){ 40 String message = scanner.nextLine(); 41 sendMessage(message); 42 } 43 } 44 45 public static void main(String[] args) { 46 int port = 10001; 47 TcpClient client = new TcpClient(port); 48 client.inputMessage(); 49 } 50 51 }