最新学习了下win32平台以及linux平台的socket程序编写,发现2者的差别不是很大,就是传输数据的时候调用的函数不同 基本流程都是大同小异,下面贴下自己写的一个比较简单的socket程序,
服务器端:
#include <stdio.h>
#include <WINSOCK2.H>
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
return 1;
}
SOCKET socket_srv = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in soc_addrs,soc_addrc ;
int soc_lens = sizeof(sockaddr);
int soc_lenc = sizeof(sockaddr);
char buffer[100];
soc_addrs.sin_addr.S_un.S_addr = inet_addr( "127.0.0.1");
soc_addrs.sin_family = AF_INET;
soc_addrs.sin_port = htons(6000);
bind(socket_srv,(SOCKADDR *) &soc_addrs,soc_lenc);
listen(socket_srv,10);
while ( 1 )
{
SOCKET socket_client = accept(socket_srv,(SOCKADDR*)&soc_addrc,&soc_lenc);
recv(socket_client,buffer,100,0);
printf( "%s\n",buffer);
closesocket( socket_client);
}
WSACleanup();
return 0;
}
#include <WINSOCK2.H>
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
return 1;
}
SOCKET socket_srv = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in soc_addrs,soc_addrc ;
int soc_lens = sizeof(sockaddr);
int soc_lenc = sizeof(sockaddr);
char buffer[100];
soc_addrs.sin_addr.S_un.S_addr = inet_addr( "127.0.0.1");
soc_addrs.sin_family = AF_INET;
soc_addrs.sin_port = htons(6000);
bind(socket_srv,(SOCKADDR *) &soc_addrs,soc_lenc);
listen(socket_srv,10);
while ( 1 )
{
SOCKET socket_client = accept(socket_srv,(SOCKADDR*)&soc_addrc,&soc_lenc);
recv(socket_client,buffer,100,0);
printf( "%s\n",buffer);
closesocket( socket_client);
}
WSACleanup();
return 0;
}
客户端:
#include <stdio.h>
#include <WINSOCK2.H>
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
return 1;
}
struct sockaddr_in sco_addr;
int len = sizeof(SOCKADDR);
char buffer[100] = { "welcome to http://wiliiwin.blog.51cto.com/"};
sco_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
sco_addr.sin_family = AF_INET;
sco_addr.sin_port = htons(6000);
SOCKET scoke_client = socket(AF_INET,SOCK_STREAM,0);
while (1)
{
connect(scoke_client,(SOCKADDR*)&sco_addr,len);
send(scoke_client,buffer,100,0);
closesocket(scoke_client);
}
WSACleanup();
return 0;
}
#include <WINSOCK2.H>
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
return 1;
}
struct sockaddr_in sco_addr;
int len = sizeof(SOCKADDR);
char buffer[100] = { "welcome to http://wiliiwin.blog.51cto.com/"};
sco_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
sco_addr.sin_family = AF_INET;
sco_addr.sin_port = htons(6000);
SOCKET scoke_client = socket(AF_INET,SOCK_STREAM,0);
while (1)
{
connect(scoke_client,(SOCKADDR*)&sco_addr,len);
send(scoke_client,buffer,100,0);
closesocket(scoke_client);
}
WSACleanup();
return 0;
}
此程序就是客户端向服务器端发送一个字符串,服务端会接收到这个字符串,服务端要是现实出接受的字符串,就说明程序已经实现了网络通信.
本文转自wiliiwin 51CTO博客,原文链接:http://blog.51cto.com/wiliiwin/336751