Socket实现TCP编程
2018-01-05 本文已影响9人
JC_Hou
image.png
image.png
image.png
image.png
登录案例
服务器端
package com.company;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* 基于TCP协议的Socket通信,实现用户登录
* 服务器端
* @param args
*/
public static void main(String[] args) {
try {
//1、创建服务器Socket,指定端口
ServerSocket serverSocket = new ServerSocket(8888);
//2、使用accept监听,等待客户端连接,处于阻塞状态
System.out.println("服务端即将启动,等待客户连接。。。");
Socket acceptSocket = serverSocket.accept();
//3、获取输入流读取客户端发送的信息
InputStream inputStream = acceptSocket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while ((line=br.readLine())!=null){
System.out.println("我是服务器,客户端说:"+line);
}
acceptSocket.shutdownInput();
//4、响应客户端的请求
OutputStream outputStream = acceptSocket.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write("欢迎您!");
bw.flush();
acceptSocket.shutdownOutput();
//5、关闭资源
br.close();
bw.close();
acceptSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端
package com.company;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
//1、创建客户端socket,指定服务器地址和端口
Socket socket = new Socket("localhost",8888);
// Socket socket = new Socket("127.0.0.1",8888);
//2、获取输出流,发送信息
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write("admin;123");
bw.flush();
socket.shutdownOutput();
//3、获取服务器端的信息,并响应
InputStream inputStream = socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while ((line=br.readLine())!=null){
System.out.println("已收到服务器端的回复:"+line);
}
socket.shutdownInput();
//4、关闭资源
bw.close();
br.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
多线程实现服务器端
image.png修改服务端
package com.company;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* 基于TCP协议的Socket通信,实现用户登录
* 服务器端
* @param args
*/
public static void main(String[] args) {
try {
//1、创建服务器Socket,指定端口
ServerSocket serverSocket = new ServerSocket(8888);
Socket acceptSocket=null;
System.out.println("服务端即将启动,等待客户连接。。。");
int count=0;
//死循环
while (true){
//2、使用accept监听,等待客户端连接,处于阻塞状态,
// 阻塞时不会循环,当有客户端连接时循环继续
acceptSocket = serverSocket.accept();
new ServerThread(acceptSocket).run();
count++;//统计客户端数量
System.out.println("客户端的数量为"+count);
InetAddress address=acceptSocket.getInetAddress();
System.out.println("当前客户端IP地址为:"+address.getHostAddress());
}
// //死循环代码无法跑到这
// serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
线程
package com.company;
import java.io.*;
import java.net.Socket;
public class ServerThread extends Thread {
//和本线程相关的Socket
Socket mSocket = null;
public ServerThread(Socket socket) {
mSocket = socket;
}
//执行线程,响应客户端
@Override
public void run() {
BufferedReader br = null;
BufferedWriter bw = null;
//3、获取输入流读取客户端发送的信息
InputStream inputStream = null;
try {
inputStream = mSocket.getInputStream();
br = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("我是服务器,客户端说:" + line);
}
mSocket.shutdownInput();
//4、响应客户端的请求
OutputStream outputStream = mSocket.getOutputStream();
bw = new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write("欢迎您!");
bw.flush();
mSocket.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、关闭资源
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (mSocket != null) {
try {
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}