标签:
1 练习 2 /* 3 * 编写一个聊天程序,有收数据的部分,有发数据的部分 4 */ 5 package bolgtest; 6 import java.io.*; 7 import java.net.*; 8 public class UdpTest { 9 public static void main(String[] args)throws Exception { 10 DatagramSocket datagramsocket1 = new DatagramSocket(); 11 DatagramSocket datagramsocket2 = new DatagramSocket(10000); 12 13 UdpSend send = new UdpSend(datagramsocket1); 14 UdpReceive receive = new UdpReceive(datagramsocket2); 15 Thread thread1 = new Thread(send); 16 Thread thread2 = new Thread(receive); 17 thread1.start(); 18 thread2.start(); 19 20 } 21 } 22 class UdpSend implements Runnable{ 23 private DatagramSocket datagramsocket = null; 24 UdpSend(DatagramSocket socket){ 25 this.datagramsocket = socket; 26 } 27 public void run(){ 28 BufferedReader buffer = null; 29 try{ 30 buffer = new BufferedReader(new InputStreamReader(System.in)); 31 String str = null; 32 while( (str = buffer.readLine())!=null) 33 { 34 if("886".equals(str)) 35 break; 36 byte[] data = str.getBytes(); 37 DatagramPacket datagrampacket = new DatagramPacket(data,data.length,InetAddress.getByName("192.168.1.255"),10000); 38 datagramsocket.send(datagrampacket); 39 } 40 }catch(Exception e){ 41 throw new RuntimeException("出错啦"); 42 } 43 finally{ 44 try{ 45 buffer.close(); 46 }catch(Exception e){ 47 throw new RuntimeException("出错啦"); 48 } 49 } 50 } 51 } 52 53 class UdpReceive implements Runnable{ 54 private DatagramSocket datagramsocket = null; 55 UdpReceive(DatagramSocket socket){ 56 this.datagramsocket = socket; 57 } 58 public void run(){ 59 BufferedReader buffer = null; 60 while(true){ 61 try{ 62 byte[] data = new byte[1024]; 63 DatagramPacket datagrampacket = new DatagramPacket(data,data.length); 64 datagramsocket.receive(datagrampacket); 65 String str = new String(datagrampacket.getData(),0,datagrampacket.getData().length); 66 String str2 = datagrampacket.getAddress().getHostAddress(); 67 System.out.print(str2+":"+str); 68 }catch(Exception e){ 69 throw new RuntimeException("出错啦"); 70 } 71 } 72 73 74 75 } 76 }
1 练习 2 /* 3 * 把客户端的一个文件传到服务端去,并且在服务端中存储起来 4 */ 5 package bolgtest; 6 import java.io.*; 7 import java.net.*; 8 public class ClientTest { 9 public static void main(String[] args) { 10 try{ 11 Socket socket = new Socket("192.168.1.101",10000); 12 BufferedWriter buffwriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 13 BufferedReader clientreader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 14 BufferedReader buffread = new BufferedReader(new FileReader("F:\\BaiduYunDownload\\client.txt")); 15 String str =null; 16 while((str = buffread.readLine())!=null){ 17 buffwriter.write(str); 18 buffwriter.newLine(); 19 buffwriter.flush(); 20 } 21 socket.shutdownOutput(); 22 String str1 = null; 23 while((str1 = clientreader.readLine())!=null){ 24 System.out.println(str1); 25 } 26 }catch(Exception e){ 27 System.out.println(e.getMessage()); 28 } 29 30 31 32 } 33 } 34 35 package bolgtest; 36 import java.io.*; 37 import java.net.ServerSocket; 38 import java.net.Socket; 39 public class ServerTest { 40 public static void main(String[] args)throws Exception { 41 ServerSocket server = new ServerSocket(10000); 42 Socket socket = server.accept(); 43 BufferedReader buffreader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 44 BufferedWriter buffwriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 45 PrintWriter printwriter = new PrintWriter(new FileWriter("F:\\BaiduYunDownload\\copy.txt"),true); 46 String str = null; 47 while((str = buffreader.readLine())!=null){ 48 printwriter.println(str); 49 } 50 buffwriter.write("上传成功"); 51 buffwriter.newLine(); 52 buffwriter.flush(); 53 54 } 55 } 56 57 58 59 60 61 62 63 64
标签:
原文地址:http://www.cnblogs.com/yuemingxingxing/p/5078030.html