1using
system;
2using
system.collections.generic;
3using
system.componentmodel;
4using
system.data;
5using
system.drawing;
6using
system.text;
7using
system.windows.forms;89
using
system.net;
10using
system.net.sockets;
11using
system.threading;
12using
system.xml;
1314
namespace
server
1522
23private
void
servermain_load(
object
sender, eventargs e)
2428
29private
void
配置引數toolstripmenuitem_click(
object
sender, eventargs e)
3034
35private
void
關於toolstripmenuitem_click(
object
sender, eventargs e)
3640
///41
///獲得xml檔案中的埠號
42/// 43
///
44private
intgetport()
4554
catch
//預設是660055}
5657
//宣告將要用到的類
58private
ipendpoint serverinfo;
//存放伺服器的ip和埠資訊
59private
socket serversocket;
//服務端執行的socket
60private
thread serverthread;
//服務端執行的執行緒
61private
socket clientsocket;
//為客戶端建立的socket連線
62private
intclientnumb;
//存放客戶端數量
63private
byte
msgbuffer;
//存放訊息資料
6465
private
void
cmdstar_click(
object
sender, eventargs e)
6686
87//
接受客戶端連線的方法
88private
void
recieveaccept()
8997}98
99//
回發資料給客戶端
100private
void
recievecallback(iasyncresult ar)
101112
rsocket.beginreceive(msgbuffer,
0, msgbuffer.length, 0,
newasynccallback(recievecallback), rsocket);
113114
}115
}116
catch
117118
}119
120private
void
cmdstop_click(
object
sender, eventargs e)
121130
131132
133}
134}
客戶端**:
1using
system;
2using
system.collections.generic;
3using
system.componentmodel;
4using
system.data;
5using
system.drawing;
6using
system.text;
7using
system.windows.forms;89
using
system.net;
10using
system.net.sockets;
1112
namespace
client
1320
21private
ipendpoint serverinfo;
22private
socket clientsocket;
23private
byte msgbuffer;
24private
byte msgsend;
2526
private
void
clientmain_load(
object
sender, eventargs e)
2739
40private
void
cmdenter_click(
object
sender, eventargs e)
4157
catch
5861}62
63private
void
receivecallback(iasyncresult ar)
6472
catch
737778}
7980
private
void
cmdsend_click(
object
sender, eventargs e)
8188
else
8992}93
94private
void
cmdexit_click(
object
sender, eventargs e)
95102
clientsocket.close();
103104
this
.cmdsend.enabled
=false
;105
this
.cmdenter.enabled
=true
;106
this
.cmdexit.enabled
=false
;107
}108
109private
void
recievemsg_textchanged(
object
sender, eventargs e)
110113
114private
void
sendmsg_keydown(
object
sender, keyeventargs e)
115121
}122
123124
125126
}127}
我只對伺服器端的**做了注釋,客戶端就沒有寫注釋了,因為**是差不多的。區別在於客戶端不需要監聽,也不需要啟用執行緒進行委託。
關於 serversocket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);
這句**,我想給初學者解釋一下,這裡「addressfamily.internetwork」表示的是使用ipv4地 址,「sockettype.stream」表示使用的是流格式(另外還有資料報格式和原始套接字格式),「protocoltype.tcp」表示使用 tcp協議(另外還有很多其它協議,例如大家常看到的udp協議)。
另外關於socket類中的beginreceive方法,請大家參考msdn,裡面有詳細說明。
黃聰 C 下如何實現伺服器 客戶端的聊天程式
1using system 2using system.collections.generic 3using system.componentmodel 4using system.data 5using system.drawing 6using system.text 7using system...
Linux客戶端與伺服器相互實現聊天功能
這個實現方法只能夠單執行緒聊天,recv是阻塞的,你發一句我回一句,必須一來一回,後文會記錄多執行緒實現聊天 客戶端 如下 include include include include include include include include int main int arg,char ar...
python socket 實現伺服器 客戶端通訊
伺服器 usr env bin python coding utf 8 import socket server def server sock socket.socket socket.af inet,socket.sock stream sock.bind localhost 8000 sock...