2014/9/29

人盡茶涼 提交于 2020-01-04 17:04:07

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 wori
25 {
26     public static void main(String args[])throws Exception
27     {
28         Socket s = new Socket("127.0.0.1", 6666);            //连接本地ip地址,链接6666端口
29         DataOutputStream dos = new DataOutputStream(s.getOutputStream());  //连接上面的s
30         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 wori
39 {
40     public static void main(String args[])throws Exception
41     {
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到末转换为String
16         }
17     }
18 }
19 
20 
21 
22 package hello;
23 import java.io.*;
24 import java.net.*;
25 public class helloworld
26 {
27     public static void main(String[] args)throws Exception
28     {
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 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!