簡單的Socket示例

2021-08-08 02:08:01 字數 2965 閱讀 8373

命令列引數:

server.exe portnumber,例如server 8000

client.exe ipaddress portnumber,例如client 127.0.0.1 8000

然後在客戶端的命令列輸入字串並回車,客戶端將會把訊息傳送到伺服器。伺服器再把訊息傳回客戶端。

伺服器端,server.cpp:

#include

#include

#include

#pragma comment(lib,"ws2_32.lib")

int main(int argc, char* argv)

//把埠號轉化成整數

short port;

if((port = atoi(argv[1]))==0)

wsadata wsa;

//初始化套接字dll

if(wsastartup(makeword(2,2),&wsa)!=0)

//建立套接字

socket serversocket;

if((serversocket=socket(af_inet,sock_stream,ipproto_tcp))==invalid_socket)

struct sockaddr_in serveraddress;

memset(&serveraddress,0,sizeof(sockaddr_in));

serveraddress.sin_family=af_inet;

serveraddress.sin_addr.s_un.s_addr = htonl(inaddr_any);

serveraddress.sin_port = htons(port);

//繫結

if(bind(serversocket,(sockaddr*)&serveraddress,sizeof(serveraddress))==socket_error)

//進入偵聽狀態

if(listen(serversocket,somaxconn)==socket_error)

printf("server %d is listening......\n",port);

socket clientsocket;//用來和客戶端通訊的套接字

struct sockaddr_in clientaddress;//用來和客戶端通訊的套接字位址

memset(&clientaddress,0,sizeof(clientaddress));

int addrlen = sizeof(clientaddress);

//接受連線

if((clientsocket=accept(serversocket,(sockaddr*)&clientaddress,&addrlen))==invalid_socket)

printf("accept connection from %s\n",inet_ntoa(clientaddress.sin_addr));

char buf[4096];

while(1)

buf[bytes]='\0';

printf("message from %s: %s\n",inet_ntoa(clientaddress.sin_addr),buf);

if(send(clientsocket,buf,bytes,0)==socket_error)

}

//清理套接字占用的資源

wsacleanup();

return 0;

}客戶端,cilent.cpp:

#include

#include

#include

#pragma comment(lib,"ws2_32.lib")

int main(int argc, char* argv)

//把字串的ip位址轉化為u_long

unsigned long ip;

if((ip=inet_addr(argv[1]))==inaddr_none)

//把埠號轉化成整數

short port;

if((port = atoi(argv[2]))==0)

printf("connecting to %s:%d......\n",inet_ntoa(*(in_addr*)&ip),port);

wsadata wsa;

//初始化套接字dll

if(wsastartup(makeword(2,2),&wsa)!=0)

//建立套接字

socket sock;

if((sock=socket(af_inet,sock_stream,ipproto_tcp))==invalid_socket)

struct sockaddr_in serveraddress;

memset(&serveraddress,0,sizeof(sockaddr_in));

serveraddress.sin_family=af_inet;

serveraddress.sin_addr.s_un.s_addr = ip;

serveraddress.sin_port = htons(port);

//建立和伺服器的連線

if(connect(sock,(sockaddr*)&serveraddress,sizeof(serveraddress))==socket_error)

char buf[4096];

while(1)

int bytes;

if((bytes=recv(sock,buf,sizeof(buf),0))==socket_error)

buf[bytes]='\0';

printf("message from %s: %s\n",inet_ntoa(serveraddress.sin_addr),buf);

}//清理套接字占用的資源

wsacleanup();

return 0;

}

簡單socket通訊示例

int socket int domain,int type,int protocol domain 位址族 af unix,af local local communication unix域協議族 af inet ipv4 internet protocols af inet6 ipv6 int...

PHP使用socket的簡單示例

由於php7的公升級,環境不一樣了,很多關於socket的 出了點問題。然後需要在另乙個環境下測試php7.2下socket是否有問題,就寫了個簡單的例子 根據網上查詢的資料寫了乙個很基本的server端與client端兩個php指令碼。server端 設定一些基本的變數 需要在server端執行此...

Linux下的簡單socket程式設計示例

api中用到的結構體 1.struct sockaddrstruct sockaddr sockaddr包含兩部分內容 1.短整型sa family定義了位址家族型別,這個型別會用於socket 函式2.14位元組的sa data陣列預留儲存本身的位址注意 1.原本沒有sa len2.根據sa fa...