用java语言构建一个网络服务器,实现客户端和服务器之间通信,实现客户端拥有独立线程,互不干扰。
应用多线程来实现服务器与多线程之间的通信的基本步骤
服务器端创建ServerSocket,循环调用accept()等待客户端链接
客户端创建一个Socket并请求和服务器端链接
服务器端接受客户端请求,创建socekt与该客户端建立专线链接
建立链接的socket在一个单独的线程上对话
服务器继续等待新的链接
服务器端Server.java
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 33 34 35 36 37 38 39 40 41 42 43 | package test.concurrent.socket;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by dong on 15-6-22.
* 基于TCP协议的Socket通信,实现用户登录
* 服务器端
*/
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket( 8888 );
Socket socket = null ;
int count = 0 ;
System.out.println( "***服务器即将启动,等待客户端的链接***" );
while ( true ){
socket = serverSocket.accept();
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();
}
}
}
|
登录后复制
服务器端线程处理类ServerThread.java
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | package test.concurrent.socket;
import java.io.*;
import java.net.Socket;
/**
* Created by dong on 15-6-22.
* 服务器端线程处理类
*/
public class ServerThread extends Thread {
Socket socket = null ;
public ServerThread(Socket socket){
this .socket = socket;
}
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( "我是服务器,客户端说: " +info);
}
socket.shutdownInput();
os = socket.getOutputStream();
pw = new PrintWriter(os);
pw.write( "欢迎你" );
pw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (pw != null )
pw.close();
if (os != null )
os.close();
if (is != null )
is.close();
if (isr != null )
isr.close();
if (br != null )
br.close();
if (socket != null )
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|
登录后复制
客户端Client.java
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package test.concurrent.socket;
import java.io.*;
import java.net.Socket;
/**
* Created by dong on 15-6-22.
* 客户端
*/
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( "用户名:tom; 密码:456" );
pw.flush();
socket.shutdownOutput();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String info = null ;
while ((info = br.readLine()) != null ){
System.out.println( "我是客户端:服务器说:" + info);
}
br.close();
is.close();
isr.close();
pw.close();
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
登录后复制
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。
更多java多线程实现服务器端与多客户端之间的通信相关文章请关注PHP中文网!