1 package com.gh.socket; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.net.Socket; 7 import java.net.UnknownHostException; 8 9 /** 10 * tcp客户端程序 11 * @author ganhang 12 */ 13 public class ClientDemo { 14 public static void main(String[] args) { 15 try { 16 Socket sk=new Socket("",8888); 17 System.out.println("与服务器连接成功"); 18 BufferedReader br=new BufferedReader(new InputStreamReader(sk.getInputStream())); 19 String info=br.readLine(); 20 br.close(); 21 System.out.println(info); 22 } catch (UnknownHostException e) { 23 e.printStackTrace(); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } 27 } 28 }
1 package com.gh.socket; 2 3 import java.io.BufferedWriter; 4 import java.io.IOException; 5 import java.io.OutputStreamWriter; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 9 /** 10 * tcp服务器端 11 * @author ganhang 12 * 13 */ 14 public class ServerDemo { 15 public static void main(String[] args) { 16 try { 17 ServerSocket ss=new ServerSocket(8888); 18 System.out.println("服务器已启动,正在连接。。。"); 19 Socket s=ss.accept(); 20 String info="你好,哈哈哈\n"; 21 BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 22 bw.write(info); 23 bw.close(); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } 27 } 28 }
1 package com.gh.UDP; 2 3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.DatagramSocket; 6 import java.net.InetAddress; 7 /** 8 * UDP的接收端 9 * 实现消息的接收与回复 10 * @author ganhang 11 * 12 */ 13 public class UDPClientDemo { 14 public static void main(String[] args) { 15 byte[] b=new byte[1024]; 16 DatagramPacket dp=new DatagramPacket(b, b.length);//用来接收的数据报包 17 try { 18 DatagramSocket ds=new DatagramSocket(5000);//客户端的收发套接字 19 System.out.println("接收端正在等待接收数据。。"); 20 ds.receive(dp);//接收数据,该方法会造成阻塞 21 System.out.println("接受成功,对方地址是"+dp.getAddress().getHostName()+":"+dp.getPort()); 22 System.out.println("对方说:"+new String(dp.getData())); 23 String info="你好,我是客户端"; 24 byte b1[]=info.getBytes(); 25 //用来发送的数据报包 26 DatagramPacket dp1=new DatagramPacket(b1,0,b1.length,InetAddress.getByName(""),dp.getPort()); 27 ds.send(dp1); 28 System.out.println("回复成功"); 29 ds.close(); 30 } catch (IOException e) { 31 e.printStackTrace(); 32 } 33 34 35 } 36 }
package com.gh.UDP; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * UDP发送端 * 实现消息的发送,和接收对方的回复 * 注意:用来接收的数据报包和用来发送的数据报包的构造方法是不同的 * 同时发送有大小限制,最大为64K * @author ganhang * */ public class UDPServerDemo { public static void main(String[] args) { String info ="你好,我是服务器"; byte [] b= info.getBytes(); try { DatagramSocket ds =new DatagramSocket(8000);//服务段的收发套接字 //用来发送的数据报包 DatagramPacket dp =new DatagramPacket(b,b.length, InetAddress.getByName("localhost"),5000); ds.send(dp);//发给客户端 System.out.println("消息发送完毕,等待回复..."); byte b1[]=new byte[1024]; DatagramPacket dp1=new DatagramPacket(b1,b1.length);//用来接收的数据报包 ds.receive(dp1); System.out.println("对方回复:"+new String(dp1.getData())); ds.close(); System.out.println(); } catch (IOException e) { e.printStackTrace(); } } }
原文:http://www.cnblogs.com/ganhang-acm/p/5154372.html