//socket基本程式設計
//服務端:
using system.net;
using system.net.sockets;
using system.text;
using system.threading;
thread mythread ;
socket socket;
// 清理所有正在使用的資源。
protected override void dispose( bool disposing )
catch
if( disposing ) }
base.dispose( disposing );
} public static ipaddress getserverip()
private void beginlisten()
catch(socketexception ex) }
// while(bytemessage!=null);
} //開始監聽
private void button1_click(object sender, system.eventargs e)
catch(system.exception er) }
//客戶端:
using system.net;
using system.net.sockets;
using system.text;
private void button1_click(object sender, system.eventargs e)
private void beginsend()
// while(bytemessage!=null);
} 基於tcp協議的傳送和接收端
tcp協議的接收端
using system.net.sockets ; //使用到tcplisten類
using system.threading ; //使用到執行緒
using system.io ; //使用到streamreader類
int port = 8000; //定義偵聽埠號
private thread ththreadread; //建立執行緒,用以偵聽埠號,接收資訊
private tcplistener tltcplisten; //偵聽埠號
private bool blistener = true; //設定標示位,判斷偵聽狀態
private networkstream nsstream; //建立接收的基本資料流
private streamreader srread;
private system.windows.forms.statusbar statusbar1;
private system.windows.forms.button button1;
private system.windows.forms.listbox listbox1; //從網路基礎資料流中讀取資料
private tcpclient tcclient ;
private void listen ( )
string stime = datetime.now.toshorttimestring ( ) ; //獲取接收資料時的時間
listbox1.items.add ( stime + " " + smessage ) ;
} }
catch ( system.security.securityexception ) }
//開始監聽
private void button1_click(object sender, system.eventargs e)
// 清理所有正在使用的資源。
protected override void dispose( bool disposing )
catch{}
if( disposing ) }
base.dispose( disposing );
} tcp協議的傳送端
using system.net.sockets; //使用到tcplisten類
using system.threading; //使用到執行緒
using system.io; //使用到streamwriter類
using system.net; //使用ipaddress類、iphostentry類等
private streamwriter swwriter; //用以向網路基礎資料流傳送資料
private networkstream nsstream; //建立傳送資料的網路基礎資料流
private tcpclient tcpclient;
private system.windows.forms.button button1;
private system.windows.forms.textbox textbox1;
private system.windows.forms.button button2;
private system.windows.forms.textbox textbox2;
private system.windows.forms.statusbar statusbar1;
private system.windows.forms.label label1;
private system.windows.forms.label label2; //通過它實現向遠端主機提出tcp連線申請
private bool tcpconnect = false; //定義識別符號,用以表示tcp連線是否建立
//連線
private void button1_click(object sender, system.eventargs e)
catch //判斷給定的ip位址的合法性
iphostentry iphost ;
try
string shostname = iphost.hostname ;
try
catch }
//傳送
private void button2_click(object sender, system.eventargs e)
else }
// 清理所有正在使用的資源。
protected override void dispose( bool disposing )
if( disposing ) }
base.dispose( disposing );
}
c socket程式設計
對於socket在這裡我不想究其歷史,我只想說其時它是一種程序通訊的方式,簡言之就是呼叫這個網路庫的一些api函式就能實現分布在不同主機的相關程序之間的資料交換.socket中首先我們要理解如下幾個定義概念 二是埠號 用來標識本地通訊程序,方便os提交資料.就是說程序指定了對方程序的網路ip,但這個...
C Socket程式設計
socket程式設計的一般步驟 伺服器端 第一步 建立乙個用於監聽連線的socket對像 第二步 用指定的埠號和伺服器的ip建立乙個endpoint對像 第三步 用socket對像的bind 方法繫結endpoint 第四步 用socket對像的listen 方法開始監聽 第五步 接收到客戶端的連線...
c socket程式設計
分別建兩個工程。把cpp拷貝進去執行就可以了。server端 include include pragma comment lib,ws2 32.lib void main else socket sersocket socket af inet,sock stream,0 建立了可識別套接字 需要...