下面的代码是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.}