導航:首頁 > 配伺服器 > 如何用多線程實現多個伺服器

如何用多線程實現多個伺服器

發布時間:2024-06-25 02:07:02

❶ SOKET緙栫▼涓涓瀹㈡埛絝榪炴帴澶氫釜鏈嶅姟鍣ㄧ殑闂棰

鍙傝冧笂涓涓鏂規硶銆

涓涓瀹㈡埛絝錛屾垜鍙浠ョ畝鍗曠殑鐞嗚В鎴愪竴涓榪涚▼銆
涓涓榪涚▼鍙浠ュ緢澶氫釜 SOCKET銆
涓涓猄OCKET 鍙浠ヨ繛鎺ヤ竴涓鏈嶅姟鍣錛屽緩絝嬩竴涓榪炴帴銆
鏂版墜錛屽緩璁灝辯敤CSocket鍚э紝 閽堝規瘡涓鏈嶅姟鍣ㄥ疄渚嬪寲涓涓瀵硅薄銆

鑷充簬SOCKET妯″瀷錛屼笉鎬ワ紝絳夊熀鏈鐨勪細浜嗭紝鍐嶆繁鍏ャ

❷ 濡備綍鐢╨ibevent瀹炵幇澶氱嚎紼嬬殑鏈嶅姟鍣

鍩烘湰鎬濇兂灝辨槸涓葷嚎紼嬪惎鍔ㄧ洃鍚錛屽緱鍒頒簨浠跺悗浜ょ粰宸ヤ綔綰跨▼澶勭悊
鍙浠ュ弬鑰僲emcache鐨勬簮鐮侊紝瀹冪殑io灝辨槸涓涓闈炲父鍏稿瀷鐨勫熀浜巐ibevent鐨勫氱嚎紼媔o鏈嶅姟

❸ 用java多線程實現伺服器與客戶端原理

伺服器端:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class OneToMoreServer extends JFrame implements ActionListener{
JPanel contentPane;
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField("4700");
JButton jButton1 = new JButton();
JLabel jLabel3 = new JLabel();
JTextField jTextField3 = new JTextField();
JButton jButton2 = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
ServerSocket server = null;
Socket socket = null;BufferedReader instr =null;PrintWriter os=null ;
Vector vector=new Vector();
boolean serverRun=true;
boolean clientRun=true;
//Construct the frame
public OneToMoreServer() {
jbInit();

}

class MyThread extends Thread{//該線程負責接收數據
Socket socketI=null;
BufferedReader br=null;
public MyThread(Socket socket)
{
socketI=socket;
}
public void run(){
try{
while(clientRun){
this.sleep(100);
br= new BufferedReader(new InputStreamReader(socketI.getInputStream()));
if(br.ready()){ //檢查是否有數據
jTextArea1.append("接收到來自客戶端("+socketI.getInetAddress().toString()+")的消息: "+br.readLine()+"\n");
}
}
}catch(Exception ex){JOptionPane.showMessageDialog(null,ex.toString());}
}
}

public void actionPerformed(ActionEvent e){
if(e.getSource()==jButton1){
int port=Integer.parseInt(jTextField2.getText().trim());
//監聽指定埠
try
{
server = new ServerSocket(port);
new Thread(new ListenClient()).start();
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null,ex.toString());
}
}
if(e.getSource()==jButton2){
String msg=jTextField3.getText().trim();
if(msg.length()!=0)
sendData("hello");
}
}
//該線程負責監聽指定埠
class ListenClient implements Runnable
{
public void run()
{
try{
if(jButton1.getText().trim().equals("偵聽")){
jButton1.setText("正在偵聽...");
while(serverRun)
{
Socket socketI=server.accept();//有客戶端連入時建立一個線程監聽客戶端發送的消息
vector.add(socketI);
jButton1.setText("正在聊天...");
jTextArea1.append("客戶端"+socketI.getInetAddress().toString()+"已經連接到伺服器\n");
MyThread t=new MyThread(socketI);
t.start();
}
}
}catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.toString());
}
}
}

private void sendData(String s){//發送數據
try{
for(int i=0;i<vector.size();i++)
{
//怎麼廣播?????
//向每個客戶端發送一條消息
Socket socket=(Socket)vector.get(i);
os= new PrintWriter(socket.getOutputStream());
os.println(s);
os.flush();
}
}catch(Exception ex){
}
}

private void jbInit() {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(540, 340));
this.setTitle("伺服器");
jLabel2.setBounds(new Rectangle(22, 27, 72, 28));
jLabel2.setText("埠號");
jLabel2.setFont(new java.awt.Font("宋體", 0, 14));
jTextField2.setBounds(new Rectangle(113, 27, 315, 24));

jButton1.setBounds(new Rectangle(440, 28, 73, 25));
jButton1.setFont(new java.awt.Font("Dialog", 0, 14));
jButton1.setBorder(BorderFactory.createEtchedBorder());
jButton1.setActionCommand("jButton1");
jButton1.setText("偵聽");
jLabel3.setBounds(new Rectangle(23, 57, 87, 28));
jLabel3.setText("請輸入信息");
jLabel3.setFont(new java.awt.Font("宋體", 0, 14));
jTextField3.setBounds(new Rectangle(114, 60, 314, 24));
jTextField3.setText("");
jButton2.setText("廣播");
jButton2.setActionCommand("jButton1");
jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setFont(new java.awt.Font("Dialog", 0, 14));
jButton2.setBounds(new Rectangle(440, 58, 73, 25));
jScrollPane1.setBounds(new Rectangle(23, 92, 493, 189));

contentPane.add(jTextField2, null);
contentPane.add(jButton1, null);
contentPane.add(jLabel3, null);
contentPane.add(jTextField3, null);
contentPane.add(jButton2, null);
contentPane.add(jScrollPane1, null);
contentPane.add(jLabel2, null);
jScrollPane1.getViewport().add(jTextArea1, null);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try{
socket.close();
instr.close();
System.exit(0);
}catch(Exception ex){
//JOptionPane.showMessageDialog(null,ex.toString());
}

}
});
}
public static void main(String arg[]){
JFrame.(true);
OneToMoreServer frm=new OneToMoreServer();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
}

客戶端
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Client extends JFrame implements ActionListener{
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField("127.0.0.1");
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField("4700");
JButton jButton1 = new JButton();
JLabel jLabel3 = new JLabel();
JTextField jTextField3 = new JTextField();
JButton jButton2 = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
BufferedReader instr =null;
Socket socket = null;
PrintWriter os=null;
public Client() {
jbInit();
}
class MyThread extends Thread{
public void run(){
try{
os=new PrintWriter(socket.getOutputStream());
instr=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true)
{
this.sleep(100);
if(instr.ready())
{
jTextArea1.append("接收到來自伺服器的消息: "+instr.readLine()+"\n");
}
}
}catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.toString());
}
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jButton1){
String ip=jTextField3.getText().trim();
int port=Integer.parseInt(jTextField2.getText().trim());
connectServer(ip,port);
}
if(e.getSource()==jButton2){
String s=this.jTextField3.getText().trim();
sendData(s);
}
}
private void connectServer(String ip,int port){//連接
try{
if(jButton1.getText().trim().equals("連接")){
jButton1.setText("連接伺服器...");
socket=new Socket(ip,port);
jButton1.setText("正在聊天");
MyThread t=new MyThread();
t.start();
}
}catch(Exception ex){
JOptionPane.showMessageDialog(this,ex.toString());
}
}
private void sendData(String s){//發送數據
try{
os = new PrintWriter(socket.getOutputStream());
os.println(s);
os.flush();
this.jTextArea1.append("向伺服器發送消息:"+s+"\n");
}catch(Exception ex){
JOptionPane.showMessageDialog(this,ex.toString());
}
}
private void jbInit() {
contentPane = (JPanel) this.getContentPane();
jLabel1.setFont(new java.awt.Font("宋體", 0, 14));
jLabel1.setText("伺服器名稱");
jLabel1.setBounds(new Rectangle(20, 22, 87, 28));
contentPane.setLayout(null);
this.setSize(new Dimension(540, 340));
this.setTitle("客戶端");

jTextField1.setBounds(new Rectangle(114, 26, 108, 24));
jLabel2.setBounds(new Rectangle(250, 25, 72, 28));
jLabel2.setText("埠號");
jLabel2.setFont(new java.awt.Font("宋體", 0, 14));
jTextField2.setBounds(new Rectangle(320, 27, 108, 24));

jButton1.setBounds(new Rectangle(440, 28, 73, 25));
jButton1.setFont(new java.awt.Font("Dialog", 0, 14));
jButton1.setBorder(BorderFactory.createEtchedBorder());
jButton1.setActionCommand("jButton1");
jButton1.setText("連接");
jLabel3.setBounds(new Rectangle(23, 57, 87, 28));
jLabel3.setText("請輸入信息");
jLabel3.setFont(new java.awt.Font("宋體", 0, 14));
jTextField3.setBounds(new Rectangle(114, 60, 314, 24));

jButton2.setText("發送");
jButton2.setActionCommand("jButton1");
jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setFont(new java.awt.Font("Dialog", 0, 14));
jButton2.setBounds(new Rectangle(440, 58, 73, 25));
jScrollPane1.setBounds(new Rectangle(23, 92, 493, 189));

contentPane.add(jLabel1, null);
contentPane.add(jTextField1, null);
contentPane.add(jLabel2, null);
contentPane.add(jTextField2, null);
contentPane.add(jButton1, null);
contentPane.add(jLabel3, null);
contentPane.add(jTextField3, null);
contentPane.add(jButton2, null);
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(jTextArea1, null);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try{
socket.close();instr.close();os.close();System.exit(0);
}catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.toString());
}
}
});
}
public static void main(String arg[]){
JFrame.(true);
Client frm=new Client();
frm.setVisible(true);
}
}

❹ 關於多線程伺服器

你說你綁定和監聽都好了吧
那麼服務就可以這樣寫
while(bWorking)
{
SOCKET sAccept = accept(sListen, NULL, NULL);
if(sAccept != SOCKET_ERROR)
CreateThread(NULL, 0, ThreadProc, (LPVOID)&sAccept, 0, NULL);
}
相應的線程函數可以這樣:
DWORD WINAPI ThreadProc(LPVOID lpPara)
{
SOCKET sComm = *((SOCKET*)lpPara);
//從這里開始讀寫這個socket就可以了
}
這樣就實現了accept一個連接就創建一個線程。

閱讀全文

與如何用多線程實現多個伺服器相關的資料

熱點內容
速騰汽車怎麼給安卓手機充電 瀏覽:270
蘋果安卓換機用什麼軟體好 瀏覽:261
華為通話加密電話 瀏覽:60
什麼伺服器可以代替計算機 瀏覽:962
伺服器當電腦用怎麼安裝顯卡 瀏覽:521
回女友消息神器python 瀏覽:52
App分類框怎麼添加 瀏覽:546
海爾帥王子冰箱壓縮機啟動跳閘 瀏覽:488
cadfill命令什麼意思 瀏覽:336
中公專項題庫pdf 瀏覽:933
拼多多緩存在哪個文件夾 瀏覽:643
安卓無卡汽車導航怎麼升級 瀏覽:50
cs管理員命令是什麼 瀏覽:126
華為v10模擬加密門禁卡 瀏覽:725
android修改app名稱 瀏覽:640
輻射3夥伴命令 瀏覽:247
蘋果電腦pdf轉換成word 瀏覽:921
純小數運演算法則 瀏覽:385
php什麼是事務 瀏覽:859
怎麼添加網名文件夾 瀏覽:771