簡易測試程式,用於測試使用socket 實現udp.
程式結構簡單,僅有相關函式的使用.旨在明確新手對於socket的學習方向.
伺服器程式:
//#include "stdafx.h"
#include #include #include using namespace std;
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv)
cout << "initialize ws2_32.lib...done..." << endl;
socket srv_socket;
srv_socket = socket(af_inet,sock_dgram,ipproto_udp);
if (srv_socket == socket_error)
cout << "create socket...done..." << endl;
sockaddr_in srv_addr;
srv_addr.sin_addr.s_un.s_addr = htonl(inaddr_any);
srv_addr.sin_family = af_inet;
srv_addr.sin_port = htons(6000);
ret = bind(srv_socket,(sockaddr *)&srv_addr,sizeof(srv_addr));
if (ret == socket_error)
socket client_socket;
client_socket = socket(af_inet,sock_dgram,0);
if (client_socket == socket_error)
//²»ðèòªbind
//·þîñæ÷¶ëµäµøö·ºí¶ë¿ú
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);
char send_buf[256];
sprintf(send_buf,"hello.....");
ret = sendto(client_socket,send_buf,strlen(send_buf) + 1,0,(sockaddr*)&addrsrv,sizeof(sockaddr));
if (ret == socket_error)
cout << "done..." << endl;
closesocket(client_socket);
wsacleanup();
return 0;
}
Socket程式設計 簡易測試socket TCP程式
簡易的利用socket 實現tcp連線的程式,旨在簡化程式結構,給初學者乙個啟示的作用.伺服器端程式 include stdafx.h include include include using namespace std pragma comment lib,ws2 32.lib 新增ws2 32...
Socket 連線簡易程式設計
客戶端 include include using namespace std pragma comment lib,ws2 32.lib define port 4000 define ip address 127.0.0.1 int main int agrc,char agrv create ...
linux下簡易socket程式設計
最近研究了一下linux下的socket程式設計。這裡寫乙個簡易的版本上去。主要是分client和server兩個部分。對於client比較簡單,這裡僅做乙個簡單的client。可以給自己的server傳送一條訊息並且可以得到一條伺服器返回的訊息,之後結束程序。include include inc...