关于TCP的两个小练习_第一个博客~

先来一个本地的,客户端发送请求,服务端接收请求的简单代码

 1 package com.TCP.java;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.InetAddress;
7 import java.net.ServerSocket;
8 import java.net.Socket;
9 import org.junit.Test;
10
11 //客户端给服务端发送信息。服务端输出此信息到控制台上
12 public class TestTCP {
13 // 客户端
14 @Test
15 public void client() {
16 Socket socket = null;
17 OutputStream os = null;
18 try {
19 // 1.创建一个Socket的对象,通过构造器指明服务器端的IP地址,以及其接收程序的端口号
20 socket = new Socket(InetAddress.getByName("192.168.1.101"), 9090);
21 // 2.getOutputStream():发送数据,方法返回OutputStream的对象
22 os = socket.getOutputStream();
23 // 3.具体的输出过程
24 os.write("我是客户端".getBytes());
25 } catch (IOException e) {
26 e.printStackTrace();
27 } finally {
28 // 关闭相应的流和Socket对象
29 try {
30 if (os != null) {
31 os.close();
32 }
33 if (socket != null) {
34 socket.close();
35 }
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 }
40 }
41
42 // 服务端
43 @Test
44 public void server() {
45 ServerSocket ss = null;
46 Socket s = null;
47 InputStream is = null;
48 try {
49 // 1.创建一个ServerSocket的对象,通过构造器指明自身的端口号
50 ss = new ServerSocket(9090);
51 // 2.调用其accept()方法,返回一个Socket对象
52 s = ss.accept();
53 // 3.调用Socket对象的getInputStream()方法,获取一个从客户端发送过来的输入流
54 is = s.getInputStream();
55 // 4.对获取的流进行操作
56 byte[] b = new byte[20];
57 int length;
58 while ((length = is.read(b)) != -1) {
59 String str = new String(b, 0, length);
60 System.out.println(str);
61 }
62 System.out.println("收到来自于" + s.getInetAddress().getHostName() + "的连接");
63 } catch (IOException e) {
64 e.printStackTrace();
65 } finally {
66 // 关闭相应的流,Socket,ServerSocket对象
67 if (is != null) {
68 try {
69 is.close();
70 } catch (IOException e) {
71 e.printStackTrace();
72 }
73 }
74 if (s != null) {
75 try {
76 s.close();
77 } catch (IOException e) {
78 e.printStackTrace();
79 }
80 }
81 if (ss != null) {
82 try {
83 ss.close();
84 } catch (IOException e) {
85 e.printStackTrace();
86 }
87 }
88 }
89 }
90 }

再来一个稍复杂的,在服务器收到客户端发送的请求时,给一个友好的回馈

 package com.TCP.java;

 import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException; import org.junit.Test; //客户端给服务端发送信息,服务端打印信息到控制台上,同时发送“已收到信息”给客户端
public class TestTCP2 {
//客户端
@Test
public void client(){
Socket s = null;
OutputStream os = null;
InputStream is = null;
try {
s = new Socket(InetAddress.getByName("192.168.1.101"),9797);
os = s.getOutputStream();
os.write("我是客户端".getBytes()); //shutdownOutput():执行此方法,可以显式的告诉服务端发送完毕
s.shutdownOutput(); is = s.getInputStream();
byte[] b = new byte[20];
int length;
while((length = is.read(b)) != -1){
String str = new String(b,0,length);
System.out.println(str);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//服务端
@Test
public void server(){
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
OutputStream os = null;
try {
ss = new ServerSocket(9797);
s = ss.accept();
is = s.getInputStream(); byte[] b = new byte[20];
int length;
while((length = is.read(b)) != -1){
String str = new String(b, 0, length);
System.out.println(str);
} os = s.getOutputStream();
os.write("已收到请求".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ss != null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
上一篇:Js控制iphone端的input/textarea元素失去焦点时隐藏键盘


下一篇:Mongodb 服务(windows环境下)因被强制关闭,导致服务不能启动的处理办法