基於tcp的socket程式設計
伺服器端程式: 1
、建立socket
2、將套接字繫結到乙個本地位址和埠上
bind
3、將套接字設為監聽模式,準備接受客戶請求
listen
4、等待客戶請求到來;當請求到來後,結合搜此次連線的套接字
accept
5、用返回的套接字和客戶端進行通訊
send/recv
6、返回,等待另一客戶請求。 7
、關閉套接字。
原始碼:#pragma comment(lib, "ws2_32.lib ")
#include
#include
void main()
/* confirm that the winsock dll supports 2.2.*/
/* note that if the dll supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wversion since that is the version we */
/* requested. */
if ( lobyte( wsadata.wversion ) != 2 ||
hibyte( wsadata.wversion ) != 2 )
socket socksrv=socket(af_inet,sock_stream,0);
sockaddr_in addrsrv;
addrsrv.sin_addr.s_un.s_addr=htonl(inaddr_any);
addrsrv.sin_family=af_inet;
addrsrv.sin_port=htons(6000);
bind(socksrv,(sockaddr*)&addrsrv,sizeof(sockaddr));
listen(socksrv,5);
sockaddr_in addrclient;
int len=sizeof(sockaddr);
while(1)}
客戶端程式: 1
、建立套接字
socket
2、想伺服器發出連線請求
connect
3、和伺服器進行通訊
send/recv
4、關閉套接字。
原始碼:#pragma comment(lib, "ws2_32.lib ")
#include
#include
void main()
/* confirm that the winsock dll supports 2.2.*/
/* note that if the dll supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wversion since that is the version we */
/* requested. */
if ( lobyte( wsadata.wversion ) != 2 ||
hibyte( wsadata.wversion ) != 2 )
socket sockclient=socket(af_inet,sock_stream,0);
sockaddr_in addrsrv;
addrsrv.sin_addr.s_un.s_addr=inet_addr("127.0.0.1");
addrsrv.sin_family=af_inet;
addrsrv.sin_port=htons(6000);
connect(sockclient,(sockaddr*)&addrsrv,sizeof(sockaddr));
char recvbuf[100];
recv(sockclient,recvbuf,100,0);
printf("%s/n",recvbuf);
send(sockclient,"this is zhouyang",strlen("this is zhouyang")+1,0);
closesocket(sockclient);
wsacleanup();}
基於udp的socket程式設計
伺服器端: 1
、建立socket
2、將套接字繫結到乙個本地位址和埠上
bind
3、等待接受資料
receive from
4、關閉套接字。
**:#include
#include
void main()
/* confirm that the winsock dll supports 2.2.*/
/* note that if the dll supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wversion since that is the version we */
/* requested. */
if ( lobyte( wsadata.wversion ) != 2 ||
hibyte( wsadata.wversion ) != 2 )
socket socketsrv=socket(af_inet,sock_dgram,0);
sockaddr_in addrsrv;
addrsrv.sin_addr.s_un.s_addr=htonl(inaddr_any);
addrsrv.sin_family=af_inet;
addrsrv.sin_port=htons(6000);
bind(socketsrv,(sockaddr*)&addrsrv,sizeof(sockaddr));
sockaddr_in addrclient;
int len=sizeof(sockaddr);
char recvbuf[100];
recvfrom(socketsrv,recvbuf,100,0,(sockaddr*)&addrclient,&len);
printf("%s/n",recvbuf);
closesocket(socketsrv);
wsacleanup();}
客戶端程式: 1
、建立套接字
socket
2、向伺服器傳送資料 3
、關閉套接字
**:#include
#include
void main()
/* confirm that the winsock dll supports 2.2.*/
/* note that if the dll supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wversion since that is the version we */
/* requested. */
if ( lobyte( wsadata.wversion ) != 2 ||
hibyte( wsadata.wversion ) != 2 )
socket sockclient=socket(af_inet,sock_dgram,0);
sockaddr_in addrsrv;
addrsrv.sin_addr.s_un.s_addr=inet_addr("127.0.0.1");
addrsrv.sin_family=af_inet;
addrsrv.sin_port=htons(6000);
sendto(sockclient,"hello visionsky",
strlen("hello visionsky")+1,0,
(sockaddr*)&addrsrv,sizeof(sockaddr));
closesocket(sockclient);
wsacleanup(); }
注意:先啟動伺服器端,在啟動客戶端,另還需新增
ws2_32.lib~
vc下socket程式設計步驟
vc下網路程式設計需注意 1.加上鏈結檔案ws2 32.lib 2.標頭檔案包含 winsock.h 編寫步驟如下 sockets 套接字 程式設計有三種 流式套接字 sock stream 資料報套接字 sock dgram 原始套接字 sock raw 基於tcp的socket程式設計是採用的流...
VC網路程式設計 Socket套接字程式設計
基於tcp的socket程式設計 tcp伺服器 include 包含標頭檔案 include include pragma comment lib,ws2 32.lib 顯式連線套接字型檔 int main 主函式開始 closesocket s 關閉套接字控制代碼 closesocket s1 w...
VC 中Socket程式設計的實現 UDP客戶端
預備 載入套接字型檔 include include 還需要新增鏈結庫ws2 32.lib 工程 設定 鏈結 物件 庫模組 word wversionrequested 準備載入winsock庫的版本,注意高位元組是副版本號 wsadata wsadata 是乙個返回值,指向wsadata結構的指標...