//服務端
usingsystem;
using
system.collections.generic;
using
system.componentmodel;
using
system.data;
using
system.drawing;
using
system.text;
using
system.windows.forms;
using
system.net;
using
system.net.sockets;
using
system.threading;
using
system.xml;
namespace
server
private
void servermain_load(object
sender, eventargs e)
private
void 配置引數toolstripmenuitem_click(object
sender, eventargs e)
private
void 關於toolstripmenuitem_click(object
sender, eventargs e)
//////
獲得xml檔案中的埠號
/// ///
private
intgetport()
catch //
預設是6600}
//宣告將要用到的類
private ipendpoint serverinfo;//
存放伺服器的ip和埠資訊
private socket serversocket;//
服務端執行的socket
private thread serverthread;//
服務端執行的執行緒
private socket clientsocket;//
為客戶端建立的socket連線
private
int clientnumb;//
存放客戶端數量
private
byte msgbuffer;//
存放訊息資料
private
void cmdstar_click(object
sender, eventargs e)
//接受客戶端連線的方法
private
void
recieveaccept()
}//回發資料給客戶端
private
void
recievecallback(iasyncresult ar)
rsocket.beginreceive(msgbuffer,
0, msgbuffer.length, 0, new
asynccallback
(recievecallback), rsocket);}}
catch
}private
void cmdstop_click(object
sender, eventargs e)
}}
//客戶端
//客戶端
using
system;
using
system.collections.generic;
using
system.componentmodel;
using
system.data;
using
system.drawing;
using
system.text;
using
system.windows.forms;
using
system.net;
using
system.net.sockets;
namespace
client
private
ipendpoint serverinfo;
private
socket clientsocket;
private
byte msgbuffer;
private
byte msgsend;
private
void clientmain_load(object
sender, eventargs e)
private
void cmdenter_click(object
sender, eventargs e)
catch
}private
void
receivecallback(iasyncresult ar)
catch
}private
void cmdsend_click(object
sender, eventargs e)
else
}private
void cmdexit_click(object
sender, eventargs e)
clientsocket.close();
this.cmdsend.enabled = false
;
this.cmdenter.enabled = true
;
this.cmdexit.enabled = false
; }
private
void recievemsg_textchanged(object
sender, eventargs e)
private
void sendmsg_keydown(object
sender, keyeventargs e)}}
}
通訊 C 多執行緒Socket 檔案傳輸
服務端 using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.text using system.wi...
socket多執行緒通訊
程序與執行緒的區別 程序 在作業系統構成單獨執行流的單位。執行緒 在程序構成單獨執行流的單位 執行緒的建立和執行流程 include int pthread create pthread t restrict thread,const pthread attr t restrict attr,voi...
多執行緒socket通訊client
控制台下實現多執行緒socket通訊,服務端為每個請求的客戶端建立乙個執行緒,進行通訊。其實這些msdn裡都有 一.基本流程如下 定義變數 獲得winsock版本 載入winsock庫 初始化 建立套接字 設定套接字選項 關閉套接字 解除安裝winsock庫 釋放資源 二.客戶端 建立過程 下篇是服...