SOCKET 程式設計TCP IP UDP

2022-08-28 12:39:09 字數 3275 閱讀 7069

tcp/ip

資源:server:

#include#includevoid main()

//建立套接字

printf("create socket...\n");

servsock=socket(af_inet,sock_stream,0);

//填充伺服器位址結構

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

sa.sin_family=af_inet;

sa.sin_port=htons(servport);

sa.sin_addr.s_addr=inet_addr("192.168.1.100");

"127.0.0.1");

//繫結套接字到伺服器位址結構

printf("binding...\n");

err=bind(servsock,(const sockaddr *)&sa,sizeof(sa));

if(err!=0)

//監聽套接字

printf("listening...\n");

err=listen(servsock,5);

if(err!=0)

//等待連線請求

printf("waitting request...\n");

len=sizeof(cliaddr);

clisock=accept(servsock,(struct sockaddr *)&cliaddr,&len);

len=recv(clisock,buff,sizeof(buff),0);

if(len>0)

printf("accept client:%s:%d\n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));

sprintf(buff,"welcome you %s",inet_ntoa(cliaddr.sin_addr));

//傳送歡迎詞

send(clisock,buff,strlen(buff),0);

sleep(1000);

// recv(clisock,buff,strlen(buff),0);

// printf("%s\n",buff);

//關閉連線

closesocket(clisock);

closesocket(servsock);

wsacleanup();

}

client:

#includevoid main()

//建立套接字

sock=socket(af_inet,sock_stream,0);

//定義伺服器位址結構

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

sa.sin_family=af_inet;

sa.sin_port=htons(servport);

sa.sin_addr.s_addr=inet_addr("192.168.1.100");

// 連線伺服器

err=connect(sock,(const sockaddr*)&sa,sizeof(sa));

// printf("test1");

while(1) }

//接收歡迎詞

memset(buff,0,sizeof(buff));

len=recv(sock,buff,sizeof(buff),0);

printf("%s\n",buff);

//關閉連線

closesocket(sock);

wsacleanup();

}

udp:

#include#include#include#define bufsize 256

void main(void)

sockaddr_in addrsock;

addrsock.sin_family=af_inet;

addrsock.sin_port=htons(6000);

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

int retval;

retval=bind(m_socket,(sockaddr*)&addrsock,sizeof(sockaddr));

if(socket_error==retval)

/////////////////接收///////////////////////////

sockaddr_in addrfrom;

int len=sizeof(sockaddr),len2;

char recvbuf[200];

char tempbuf[300];

sockaddr_in addrto;

addrto.sin_family=af_inet;

addrto.sin_port=htons(6001);

addrto.sin_addr.s_un.s_addr=inet_addr("192.168.1.100");

while(true)

sockaddr_in addrsock;

addrsock.sin_family=af_inet;

addrsock.sin_port=htons(6001);

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

int retval;

retval=bind(m_socket,(sockaddr*)&addrsock,sizeof(sockaddr));

if(socket_error==retval)

/////////////////接收///////////////////////////

sockaddr_in addrfrom;

int len=sizeof(sockaddr),len2;

char recvbuf[200];

char tempbuf[300];

sockaddr_in addrto;

addrto.sin_family=af_inet;

addrto.sin_port=htons(6000);

addrto.sin_addr.s_un.s_addr=inet_addr("192.168.1.100");

while(true)

//關閉socket

closesocket(m_socket);

wsacleanup();

exit(0);

}

socket程式設計

一直以為serversocket accept之後客戶端才能發資訊,實驗後得出如下結論 1 serversocket沒有accept時,client是可以傳送資訊到server端的。2 serversocket accept之後,正在處理訊息時,client也是可以傳送資訊到server端。如果se...

Socket程式設計

對tcp ip udp socket程式設計這些詞你不會很陌生吧?隨著網路技術的發展,這些詞充斥著我們的耳朵。那麼我想問 1.什麼是tcp ip udp?2.socket在 呢?3.socket是什麼呢?4.你會使用它們嗎?什麼是tcp ip udp?tcp ip transmission cont...

socket程式設計

建立socket 建立乙個 socket,它可用於在基於 tcp ip 的網路 如 internet 上通訊。socket s new socket addressfamily.internetwork,sockettype.stream,protocoltype.tcp 若要使用 udp 而不是 ...