首页 > 代码库 > 2014/9/29

2014/9/29

1

 1 package hello; 2  3 import java.io.DataInputStream; 4 import java.net.*; 5  6 public class helloworld 7 { 8     public static void main(String[] args)throws Exception 9     {10         ServerSocket s = new ServerSocket(6666);            //server端口号,用于监听11         Socket ss = s.accept();                                         //闭塞式,不收到不进行下去12         System.out.println("A is accepted;");                13         DataInputStream dis = new DataInputStream(ss.getInputStream()); //14         System.out.println(dis.readUTF());                //    15         dis.close();16         s.close();17     }18 }19 20 package ha;21 import java.io.*;22 import java.net.*;23 import java.util.*;24 public class wori25 {26     public static void main(String args[])throws Exception27     {28         Socket s = new Socket("127.0.0.1", 6666);            //连接本地ip地址,链接6666端口29         DataOutputStream dos = new DataOutputStream(s.getOutputStream());  //连接上面的s30         dos.writeUTF("wori");31         dos.close();32         s.close();33     }34 }

 

2

 1 package hello; 2 import java.io.*; 3 import java.net.*; 4 public class helloworld 5 { 6     public static void main(String[] args)throws Exception 7     { 8         ServerSocket s = new ServerSocket(6666); 9         Socket ss = s.accept();                 10         InputStreamReader is = new InputStreamReader(System.in);11         BufferedReader br = new BufferedReader(is);12         OutputStreamWriter os = new OutputStreamWriter(ss.getOutputStream());13         BufferedWriter bw = new BufferedWriter(os);14         InputStreamReader is2 = new InputStreamReader(ss.getInputStream());15         BufferedReader br2 = new BufferedReader(is2);16         String st = null;17         System.out.println(br2.readLine());18         while((st = br.readLine()) != null)19         {20             if(st.equalsIgnoreCase("exit"))21             {22                 break;23             }24             System.out.println(br2.readLine());25             bw.write(st);26         }27         bw.close();28         br2.close();29         br.close();30         s.close();31     }32 }33 34 package ha;35 import java.io.*;36 import java.net.*;37 import java.util.*;38 public class wori39 {40     public static void main(String args[])throws Exception41     {42         Socket s = new Socket("127.0.0.1", 6666);43         InputStreamReader is = new InputStreamReader(System.in);44         BufferedReader br = new BufferedReader(is);45         OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());46         BufferedWriter bw = new BufferedWriter(os);47         InputStreamReader is2 = new InputStreamReader(s.getInputStream());48         BufferedReader br2 = new BufferedReader(is2);49         System.out.println("Connceted;");50         String st;51         while((st = br.readLine()) != null)52         {53             if(st.equalsIgnoreCase("exit"))54             {55                 break;56             }57             bw.write(st);58             System.out.println(br2.readLine());59         }60         System.out.println("The programme is closed;");61         br.close();62         br2.close();63         bw.close();64         s.close();65     }66 }

3

udp

 1 package ha; 2 import java.io.*; 3 import java.net.*; 4 import java.util.*; 5 public class wori 6 { 7     public static void main(String args[])throws Exception 8     { 9         byte a[] = new byte[1024];                                     //用来存放接收的数据10         DatagramPacket dp = new DatagramPacket(a, 1024);        //11         DatagramSocket ds = new DatagramSocket(5678);            //端口号12         while(true)13         {14             ds.receive(dp);                        //接收15             System.out.println(new String(a, 0, dp.getLength()));    //将字节数组a从0到末转换为String16         }17     }18 }19 20 21 22 package hello;23 import java.io.*;24 import java.net.*;25 public class helloworld26 {27     public static void main(String[] args)throws Exception28     {29         byte a[] = (new String("wocao")).getBytes();            //将Sting转换为字节数组    30         DatagramPacket dp = new DatagramPacket(a, a.length, new InetSocketAddress("127.0.0.1", 5678));    //31         DatagramSocket ds = new DatagramSocket(9999);            //设置client的端口号32         ds.send(dp);                            //33         ds.close();34     }35 }

 

2014/9/29