标签:java serversocket 多线程 通讯简单实例(基于tcp协议)
首先是建立一个类继承Thread类
package com.zzq.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerThread extends Thread{
Socket socket = null;
/**
* 每个线程共享当前socket
* @param socket
*/
public ServerThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
//获取输入流 读取客户端信息
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
//获取输出流 发送给客户端
OutputStream os = null;
PrintWriter pw = null;
try {
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String info = null;
while((info = br.readLine())!=null){
System.out.println("服务器 client:"+info);
}
socket.shutdownInput();
os = socket.getOutputStream();
//向客户端发送信息
pw = new PrintWriter(os);
pw.write("200");
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭资源
try {
if(os!=null)
os.close();
if(pw!=null)
pw.close();
if(br!=null)
br.close();
if(isr!=null)
isr.close();
if(is!=null)
is.close();
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
然后写服务端代码
package com.zzq.socket;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerSocket {
public static void main(String[] args) {
try {
//建立服务端socket 监听8888端口
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = null;
int count = 0;
//循环获取客户端连接
while(true){
socket = serverSocket.accept();
//客户端连接 就使用 ServerThread线程 去处理socket读取和输入
ServerThread serverThread = new ServerThread(socket);
serverThread.start();
count++;
System.out.println("客户端连接数量--("+count+")");
InetAddress address = socket.getInetAddress();
System.out.println("客户端Ip "+address.getHostAddress());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
接下去是客户端
package com.zzq.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
try {
//创建客户端套接字 指定服务器地址与端口
Socket socket = new Socket("localhost",8888);
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.write("username:root password:123");
pw.flush();
socket.shutdownOutput();
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String info = null;
while((info = br.readLine())!=null){
System.out.println("服务器 client:"+info);
}
//关闭资源
br.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文出自 “The_Monster” 博客,请务必保留此出处http://zzqnobody.blog.51cto.com/7059797/1625841
Java 多线程ServerSocket通讯简单实例(基于TCP协议)
标签:java serversocket 多线程 通讯简单实例(基于tcp协议)
原文地址:http://zzqnobody.blog.51cto.com/7059797/1625841