java网络编程 多线程服务器与客户端系统架构 socket应用
程序员文章站
2022-06-06 13:58:04
...
java网络编程 多线程服务器与客户端系统架构 socket应用
最近在做一些金融业管理系统,经常要进行网络间数据的交互,其实也就是服务器端与客户端的一些信息发送与接收。系统已经完成,也就整理下使用的架构。
服务器端:做为系统的服务器是要面对多个客户端的,所以要用多线程来架设,对每个客户端的数据处理都是互不影响的,而且服务器与客户端的数据交互是同步的。
客户端:客户端的应用也就是发送信息到服务器,然后得到服务器返回的信息。
(代码很清晰,就不用多说了。)
服务器端主要有三个类:
1、服务器类:Server.java
2、服务器线程类:ServerThread.java
3、服务器代理线程类:ServerThreadAgent.java
客户端主要有两个类:
1、信息发送类:Client.java
2、信息接收类:ClientThreadAgent.java
package net.xiaoxiang.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
/**
* @author weishaoxiang
*/
public class Server
{
ServerSocket serverSocket;
ServerThread serverThread;
boolean sign = true; // true:服务器未启动 false:服务器已启动
public static void main ( String[] args )
{
Server server = new Server ( );
try
{
server.showmenu ( );
while ( true )
{
InputStreamReader reader = new InputStreamReader ( System.in );
BufferedReader br = new BufferedReader ( reader );
String chooce = br.readLine ( );
if ( "0".equals ( chooce ) )
server.exitSystem ( );
if ( "1".equals ( chooce ) )
server.startServer ( );
if ( "2".equals ( chooce ) )
server.stopServer ( );
if ( "3".equals ( chooce ) )
server.showmenu ( );
}
}
catch ( Exception e )
{
e.printStackTrace ( );
}
}
public void startServer ( )
{
if ( sign == true )
{
try
{
serverSocket = new ServerSocket ( 8888 );
serverThread = new ServerThread ( serverSocket );
serverThread.start ( );
sign = false;
System.out.println ( "\n 服务器--启动成功。\n" );
}
catch ( IOException e )
{
System.out.println ( "\n 服务器--启动失败!\n" );
}
}
else
{
System.out.println ( "\n 服务器--已经启动" );
}
}
public void stopServer ( )
{
if ( sign == false )
{
try
{
if ( serverThread != null )
{
serverThread.flag = false;// 关闭服务器线程
serverThread.serverSocket.close ( );
serverThread = null;
}
this.serverSocket.close ( );
sign = true;
System.out.println ( "服务器--关闭成功" );
}
catch ( IOException e )
{
System.out.println ( "服务器关闭出现异常!" );
e.printStackTrace ( );
}
}
else
{
System.out.println ( "服务器--未启动" );
}
}
public void exitSystem ( )
{
this.stopServer ( );
System.out.println ( "服务器线程关闭----退出程序" );
System.exit ( 0 );
}
public void showmenu ( )
{
System.out.println ( " 服务器菜单" );
System.out.println ( " 请选择输入相应菜单对应的标号" );
System.out.println ( " 1 启动服务器" );
System.out.println ( " 2 关闭服务器" );
System.out.println ( " 3 显示菜单" );
System.out.println ( " 0 关闭程序" );
}
}
package net.xiaoxiang.socket;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author weishaoxiang
*/
public class ServerThread extends Thread {
ServerSocket serverSocket;
boolean flag = true;
public ServerThread( ServerSocket serverSocket ){
this.serverSocket = serverSocket;
}
public void run( ){
while( flag ){
try{
Socket socket = serverSocket.accept( );
ServerThreadAgent serverThreadAgent = new ServerThreadAgent( socket );
serverThreadAgent.start( );
}
catch( Exception e ){
e.printStackTrace( );
}
}
}
}
package net.xiaoxiang.socket;
import java.net.Socket;
import net.xiaoxiang.utils.SocketUtil;
/**
* @author weishaoxiang
*/
public class ServerThreadAgent extends Thread
{
Socket socket;
public ServerThreadAgent ( Socket socket ){
this.socket = socket;
}
/**
* 在代理线程中进行业务处理
*/
public void run ( )
{
try
{
byte[] message = SocketUtil.readStream ( socket );// 接收信息
System.out.println ( socket.getInetAddress ( ) + "发来信息:"
+ new String ( message, "GBK" ) );
/* 在此处就可以进行自己的业务处理 */
String sendMessage = "信息已经收到。";
SocketUtil.writeStream ( sendMessage.getBytes ( ), socket );// 发送信息
}
catch ( Exception e )
{
e.printStackTrace ( );
}
}
}
package net.xiaoxiang.socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import net.xiaoxiang.utils.SocketUtil;
/**
* @author weishaoxiang
*/
public class Client
{
Socket socket;
ClientThreadAgent clientThreadAgent;
boolean sign = true;
public static void main ( String[] args )
{
Client client = new Client ( );
InputStreamReader reader = new InputStreamReader ( System.in );
BufferedReader br = new BufferedReader ( reader );
try
{
client.showmenu ( );
while ( true )
{
String menu = br.readLine ( );
if ( "0".equals ( menu ) )
{
client.exitSystem ( );
continue;
}
if ( "1".equals ( menu ) )
{
client.showmenu ( );
continue;
}
if ( "2".equals ( menu ) )
{
for ( int i = 0; i < 100; i++ )
client.send ( "发送第 " + i + " 条信息" );
continue;
}
System.out.println ( "输入错误!请查看菜单:3" );
}
}
catch ( Exception e )
{
e.printStackTrace ( );
}
}
private void showmenu ( )
{
System.out.println ( " 系统菜单" );
System.out.println ( " 请选择输入相应菜单对应的标号" );
System.out.println ( " 1-------显示菜单" );
System.out.println ( " 0-------关闭程序" );
System.out.println ( " 2----发送信息" );
}
private void exitSystem ( )
{
try
{
System.out.println ( "程序已经关闭。" );
System.exit ( 0 );
}
catch ( Exception e )
{
e.printStackTrace ( );
}
}
private void startClientServer ( )
{
try
{
socket = new Socket ( "127.0.0.1", 8888 );
clientThreadAgent = new ClientThreadAgent ( socket );
clientThreadAgent.start ( );
}
catch ( Exception ee )
{
ee.printStackTrace ( );
}
}
private void send ( String message )
{
this.startClientServer ( );
SocketUtil.writeStream ( message.getBytes ( ), socket );// 发送信息
}
}
package net.xiaoxiang.socket;
import java.net.Socket;
import net.xiaoxiang.utils.SocketUtil;
/**
* @author weishaoxiang
*/
public class ClientThreadAgent extends Thread
{
Socket socket;
public ClientThreadAgent ( Socket socket ){
this.socket = socket;
System.out.println ( "连接服务器成功 IP: " + socket.getInetAddress ( ) + " 端口:"
+ socket.getPort ( ) );
}
public void run ( )
{
try
{
byte[] message = SocketUtil.readStream ( socket );// 接收信息
System.out.println ( new String ( message, "GBK" ) );
socket.close ( );
}
catch ( Exception e )
{
e.printStackTrace ( );
}
}
}
上一篇: 「力扣数据库」196. 删除重复的电子邮箱(第六天)
下一篇: js第六篇:for循环