初见socket

socket 是基于tcp/ip的一种网络层上的传输方式,我们通常把TCP和UDP称为传输层。
所谓网络层请参考计算机七层模型(说不定哪天就写了)
socket整个流程:

服务端:

在服务端创建一个ServerSocket对象,实例化(绑定端口),保证客户端能够通过这个端口访问(端口尽量不与别的软件端口冲突,尽量选比较大的) 

创建socket对象,通过ServerSocket的accept方法进行阻塞,来等待客户端的链接(一般阻塞都是通过循环实现的)  

当有客户端链接之后可以通过BufferReader来读取客户端发送过来的消息,并且通过BufferReader的readline方法将消息提取出来。  

客户端:

在客户端创建一个Socket对象,实例化(绑定服务端的ip和端口)。  

实例化BufferWriter对像并且通过BufferWrite的write方法将信息传递出去。  

下面时示例

Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.p1.scoket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str = br.readLine();
System.out.println(str);

}
}

Client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.p1.scoket;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class Client {
public static void main(String[] args) throws IOException {
int port = 9999;
String host = "127.0.0.1";
Socket socket = new Socket(host,port);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String str = "hello!!!";
bw.write(str);
bw.flush();
socket.close();
}
}

循环实现聊天

我们使用while循环来实现多条语句发送,每次发送之后要多发送个/n来使服务器知道我们发送了一条数据。

Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.p1.scoket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"));
String str;
while ((str = br.readLine()) != null){
System.out.println("客户端说:"+str);
}
}
}

Client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.p1.scoket;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
static Scanner s = new Scanner(System.in);

public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1",9999);
System.out.println("你已经连接上了,来说点什么把");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while (true){
String str = s.next();
bw.write(str);
bw.write("\n");
bw.flush();
}
}
}

多线程来实现多人聊天

实现过程,循环每次链接都创建一个新的线程,来实现多个客户端同时链接服务器。

Server

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
package com.p1.scoket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server1 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9998);
while (true){
Socket socket = serverSocket.accept();
System.out.println("客户端"+socket.getInetAddress().getHostAddress()+"已连接");
new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"));
String str;
while ((str = br.readLine()) != null){
System.out.println(socket.getInetAddress().getHostAddress()+"说:"+str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
}

Client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.p1.scoket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client1 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
Socket socket = new Socket("127.0.0.1",9998);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"utf-8"));
while (true){
String str = sc.next();
bw.write(str);
bw.write("\n");
bw.flush();
}
}
}