先来一个本地的,客户端发送请求,服务端接收请求的简单代码
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 }
再来一个稍复杂的,在服务器收到客户端发送的请求时,给一个友好的回馈
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 java.net.UnknownHostException; 10 11 import org.junit.Test; 12 13 //客户端给服务端发送信息,服务端打印信息到控制台上,同时发送“已收到信息”给客户端 14 public class TestTCP2 { 15 //客户端 16 @Test 17 public void client(){ 18 Socket s = null; 19 OutputStream os = null; 20 InputStream is = null; 21 try { 22 s = new Socket(InetAddress.getByName("192.168.1.101"),9797); 23 os = s.getOutputStream(); 24 os.write("我是客户端".getBytes()); 25 26 //shutdownOutput():执行此方法,可以显式的告诉服务端发送完毕 27 s.shutdownOutput(); 28 29 is = s.getInputStream(); 30 byte[] b = new byte[20]; 31 int length; 32 while((length = is.read(b)) != -1){ 33 String str = new String(b,0,length); 34 System.out.println(str); 35 } 36 } catch (UnknownHostException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } catch (IOException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 if(is != null){ 44 try { 45 is.close(); 46 } catch (IOException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } 50 } 51 if(os != null){ 52 try { 53 os.close(); 54 } catch (IOException e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 57 } 58 } 59 if(s != null){ 60 try { 61 s.close(); 62 } catch (IOException e) { 63 // TODO Auto-generated catch block 64 e.printStackTrace(); 65 } 66 } 67 } 68 //服务端 69 @Test 70 public void server(){ 71 ServerSocket ss = null; 72 Socket s = null; 73 InputStream is = null; 74 OutputStream os = null; 75 try { 76 ss = new ServerSocket(9797); 77 s = ss.accept(); 78 is = s.getInputStream(); 79 80 byte[] b = new byte[20]; 81 int length; 82 while((length = is.read(b)) != -1){ 83 String str = new String(b, 0, length); 84 System.out.println(str); 85 } 86 87 os = s.getOutputStream(); 88 os.write("已收到请求".getBytes()); 89 } catch (IOException e) { 90 // TODO Auto-generated catch block 91 e.printStackTrace(); 92 } 93 finally{ 94 if(os != null){ 95 try { 96 os.close(); 97 } catch (IOException e) { 98 // TODO Auto-generated catch block 99 e.printStackTrace(); 100 } 101 } 102 if(is != null){ 103 try { 104 is.close(); 105 } catch (IOException e) { 106 // TODO Auto-generated catch block 107 e.printStackTrace(); 108 } 109 } 110 if(s != null){ 111 try { 112 s.close(); 113 } catch (IOException e) { 114 // TODO Auto-generated catch block 115 e.printStackTrace(); 116 } 117 } 118 if(ss != null){ 119 try { 120 ss.close(); 121 } catch (IOException e) { 122 // TODO Auto-generated catch block 123 e.printStackTrace(); 124 } 125 } 126 } 127 } 128 }