java Socket和Server Socket多线程编程_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java Socket和Server Socket多线程编程

java Socket和Server Socket多线程编程

 2013/12/9 12:26:04  xuehua1987  程序员俱乐部  我要评论(0)
  • 摘要:下面的代码是javasocket客户端和服务端的源码程序123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  • 标签:Server 多线程 Java 编程 socket 线程
下面的代码是java socket客户端和服务端的源码程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package net.tcp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends Thread
{
    ServerSocket server = null;
    Socket sk = null;
    BufferedReader rdr = null;
    PrintWriter wtr = null;
    public Server()
    {
        try
        {
            server = new ServerSocket(1987);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    public void run()
    {
        while (true)
            {
                System.out.println("Listenning...");
                try
                {
//                  每个请求交给一个线程去处理
                    sk = server.accept();
                    ServerThread th = new ServerThread(sk);
                    th.start();
                    sleep(1000);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                   
            }
    }
    public static void main(String [] args)
    {
        new Server().start();
    }
       
       
       
       
       
    class ServerThread extends Thread
    {
        Socket sk = null;
        public ServerThread(Socket sk)
        {
            this.sk = sk;
        }
        public void run()
        {
            try
            {
                wtr = new PrintWriter(sk.getOutputStream());
                rdr = new BufferedReader(new InputStreamReader(sk
                        .getInputStream()));
                String line = rdr.readLine();
                System.out.println("从客户端来的信息:" + line);
//              特别,下面这句得加上     “\n”,
                wtr.println("你好,服务器已经收到您的信息!'" + line + "'\n");
                wtr.flush();
                System.out.println("已经返回给客户端!");
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
               
        }
           
    }
}
客户端:
Java代码
1.package net.tcp;
2.
3.import java.io.BufferedReader;
4.import java.io.BufferedWriter;
5.import java.io.InputStreamReader;
6.import java.io.OutputStreamWriter;
7.import java.io.PrintWriter;
8.import java.net.Socket;
9.
10.public class Client extends Thread
11.{
12.
13.    Socket sk = null;
14.    BufferedReader reader = null;
15.    PrintWriter wtr = null;
16.    BufferedReader keyin = null;
17.
18.    public Client()
19.    {
20.        keyin = new BufferedReader(new InputStreamReader(System.in));
21.        try
22.        {
23.            sk = new Socket("127.0.0.1", 1987);
24.        }
25.        catch (Exception e)
26.        {
27.            e.printStackTrace();
28.        }
29.
30.    }
31.
32.    public void run()
33.    {
34.        try
35.        {
36.            reader = new BufferedReader(new InputStreamReader(sk
37.                    .getInputStream()));
38.            wtr = new PrintWriter(sk.getOutputStream());
39.            String get = keyin.readLine();
40.
41.            while (true)
42.            {
43.                if (get != null & get.length() > 0)
44.                {
45.                    wtr.println(get);
46.                    wtr.flush();
47.//                  wtr.close();
48.//                  System.out.println(get + "发送完毕");
49.                }
50.                if (reader != null)
51.                {
52.                    String line = reader.readLine();
53.                    System.out.println("从服务器来的信息:" + line);
54.                    
55.                }
56.                
57.            }
58.        }
59.        catch (Exception e)
60.        {
61.            e.printStackTrace();
62.        }
63.    }
64.http://wgyblog.com/html/166.html
65.    public static void main(String [] args)
66.    {
67.        new Client().start();
68.    }
69.}
发表评论
用户名: 匿名