1.客户端程序
SocketClientBlock.c
#pragma comment(lib, "WS2_32")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define BUF_SIZE (1024)
int main(int argc,char **argv)
{
WSADATA wsadata;
int sockfd,n;
char receline[BUF_SIZE];
struct sockaddr_in serveraddr;
//Winsows下启用socket
if(WSAStartup(MAKEWORD(1,1),&wsadata)==SOCKET_ERROR)
{
printf("WSAStartup() fail\n");
exit(0);
}
//建立socket
if((sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==SOCKET_ERROR)
{
printf("socket() fail\n");
exit(0);
}
//设置协议,IP及Port
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port=htons(1024);
serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
/*
//hostname转ip
hostent *hostEnt = gethostbyname( strRemote );
if( hostEnt != NULL )
{
lIPAddress = ((in_addr*)hostEnt->h_addr)->s_addr;
serveraddr.sin_addr.s_addr = lIPAddress;
}
else
{
serveraddr.sin_addr.s_addr = inet_addr( strRemote );
}
*/
//连接
if(connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr))==SOCKET_ERROR)
{
printf("connect() fail\n");
exit(0);
}
//读取数据并输入到标准输出
while((n=recv(sockfd,receline,BUF_SIZE,0))!=SOCKET_ERROR)
{
receline[n]=0;
if(fputs(receline,stdout)==EOF)
{
printf("fputs() error\r\n");
}
}
//没有获取数据
if(n<0)
{
printf("read() fail\n");
}
//Winsows下关闭socket
closesocket(sockfd);
WSACleanup();
exit(0);
}
[/code]
2.服务端程序,阻塞
SocketServerBlock.c
[code lang="c"]
#pragma comment(lib, "WS2_32")
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_CONNECT_NUM (10)
int main(int argc,char** argv)
{
WSADATA wsadata;
struct sockaddr_in serveraddr;
int socketflag,connectflag;
time_t time_tick;
char buff[1024];
//Winsows下启用网络
if(WSAStartup(MAKEWORD(1,1),&wsadata)==SOCKET_ERROR)
{
printf("WSAStartup() fail,%d\n", WSAGetLastError());
exit(0);
}
//新建socket
if(INVALID_SOCKET==(socketflag=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)))
{
printf("socket() fail\n");
exit(0);
}
//清零,设置协议,设置IP,设置Port
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
serveraddr.sin_port=htons(1024);
//绑定端口,监听1024端口的任何请求
if(SOCKET_ERROR==bind(socketflag,(struct sockaddr*)&serveraddr,sizeof(serveraddr)))
{
printf("bind() fail\n");
exit(0);
}
//监听端口,最大并发数MAX_CONNECT_NUM
if(SOCKET_ERROR==listen(socketflag,MAX_CONNECT_NUM))
{
printf("listen() fail\n");
exit(0);
}
//接受请求,发送主机时间
for(;;)
{
struct sockaddr clientaddr;
int iLen = sizeof(clientaddr);
printf("Waiting for connection...\n");
//接受连接
connectflag=accept(socketflag,&clientaddr,&iLen);
if(INVALID_SOCKET==connectflag)
{
printf("accept() fail\n");
exit(0);
}
//获取时间,并格式化
time_tick=time(NULL);
sprintf(buff,"From mys:\n%s",ctime(&time_tick));
//写入时间
if(SOCKET_ERROR==send(connectflag,buff,strlen(buff),0))
{
printf("send() fail\n");
exit(0);
}
//关闭连接
closesocket(connectflag);
}
//Winsows下关闭socket及网络
if(SOCKET_ERROR==closesocket(socketflag))
{
printf("closesocket() fail\n");
exit(0);
}
WSACleanup();
//退出
exit(0);
}
3.服务端程序,阻塞,有Select
SocketServerBlockSelect.c
#pragma comment(lib, "WS2_32")
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_CONNECT_NUM (10)
int main(int argc,char** argv)
{
WSADATA wsadata;
struct sockaddr_in serveraddr;
int socketflag,connectflag;
fd_set fdR;
struct timeval timeout ={1,500};
time_t time_tick;
char buff[1024];
//Winsows下启用网络
if(WSAStartup(MAKEWORD(1,1),&wsadata)==SOCKET_ERROR)
{
printf("WSAStartup() fail,%d\n", WSAGetLastError());
exit(0);
}
//新建socket
if(INVALID_SOCKET==(socketflag=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)))
{
printf("socket() fail\n");
exit(0);
}
//清零,设置协议,设置IP,设置Port
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
serveraddr.sin_port=htons(1024);
//绑定端口,监听1024端口的任何请求
if(SOCKET_ERROR==bind(socketflag,(struct sockaddr*)&serveraddr,sizeof(serveraddr)))
{
printf("bind() fail\n");
exit(0);
}
//监听端口,最大并发数MAX_CONNECT_NUM
if(SOCKET_ERROR==listen(socketflag,MAX_CONNECT_NUM))
{
printf("listen() fail\n");
exit(0);
}
//接受请求,发送主机时间
for(;;)
{
printf("Waiting for connection...\n");
FD_ZERO(&fdR);
FD_SET(socketflag, &fdR);
switch (select(0, &fdR, NULL, NULL,&timeout))
{
case SOCKET_ERROR:
printf("Socket Error...\n");
break;
case 0:
printf("Time Out Here...\n");
break;
default:
printf("Connection is coming...\n");
if (1)
{
struct sockaddr clientaddr;
int iLen = sizeof(clientaddr);
//接受连接
connectflag=accept(socketflag,&clientaddr,&iLen);
if(INVALID_SOCKET==connectflag)
{
printf("accept() fail\n");
exit(0);
}
//获取时间,并格式化
time_tick=time(NULL);
sprintf(buff,"From mys:\n%s",ctime(&time_tick));
//写入时间
if(SOCKET_ERROR==send(connectflag,buff,strlen(buff),0))
{
printf("send() fail\n");
exit(0);
}
//关闭连接
closesocket(connectflag);
}
break;
}
}
//Winsows下关闭socket及网络
if(SOCKET_ERROR==closesocket(socketflag))
{
printf("closesocket() fail\n");
exit(0);
}
WSACleanup();
//退出
exit(0);
}
4.服务端程序,非阻塞
SocketServerNonBlock.c
#pragma comment(lib, "WS2_32")
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_CONNECT_NUM (10)
int main(int argc,char** argv)
{
WSADATA wsadata;
struct sockaddr_in serveraddr;
int socketflag,connectflag;
time_t time_tick;
char buff[1024];
unsigned long flag=1;
//Winsows下启用网络
if(WSAStartup(MAKEWORD(1,1),&wsadata)==SOCKET_ERROR)
{
printf("WSAStartup() fail,%d\n", WSAGetLastError());
exit(0);
}
//新建socket
if(INVALID_SOCKET==(socketflag=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)))
{
printf("socket() fail\n");
exit(0);
}
//非阻塞方式
if(ioctlsocket(socketflag,FIONBIO,&flag)!=0)
{
printf("ioctlsocket() fail\n");
exit(0);
}
//清零,设置协议,设置IP,设置Port
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
serveraddr.sin_port=htons(1024);
//绑定端口,监听1024端口的任何请求
if(SOCKET_ERROR==bind(socketflag,(struct sockaddr*)&serveraddr,sizeof(serveraddr)))
{
printf("bind() fail\n");
exit(0);
}
//监听端口,最大并发数MAX_CONNECT_NUM
if(SOCKET_ERROR==listen(socketflag,MAX_CONNECT_NUM))
{
printf("listen() fail\n");
exit(0);
}
//接受请求,发送主机时间
for(;;)
{
struct sockaddr clientaddr;
int iLen = sizeof(clientaddr);
printf("Waiting for connection...\n");
//接受连接
connectflag=accept(socketflag,&clientaddr,&iLen);
if(INVALID_SOCKET==connectflag)
{
printf("no connection\n");
Sleep(1500);
}
else
{
printf("Connection is coming...\n");
//获取时间,并格式化
time_tick=time(NULL);
sprintf(buff,"From mys:\n%s",ctime(&time_tick));
//写入时间
if(SOCKET_ERROR==send(connectflag,buff,strlen(buff),0))
{
printf("send() fail\n");
exit(0);
}
//关闭连接
closesocket(connectflag);
}
}
//Winsows下关闭socket及网络
if(SOCKET_ERROR==closesocket(socketflag))
{
printf("closesocket() fail\n");
exit(0);
}
WSACleanup();
//退出
exit(0);
}