一个简单的循环式TCP服务器
一个简单的循环式TCP服务器,当服务器接到来自客户端的连接请求时,向客户端发出一条信息,工作在8273端口。
客户端代码用来测试。
代码如下,语言:C 环境:VC6.0
服务器端程序:
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#define WSVERS MAKEWORD(2,0)
int passivesock(const char *service, const char *transport, int qlen) ;
int main(int argc, char *argv[])
{
char s[] = "+OK.THIS IS A TEST SERVCIE";
char client_IP[20] ={'\0'};
char *service = "8273";
struct sockaddr_in fsin;
int len;
WSADATA wsadata;
SOCKET msock,ssock;
if(WSAStartup(WSVERS, &wsadata) != 0)
{
printf("WSAStartup 初始化失败");
exit(0);
}
msock = passivesock(service,"tcp",5);//5为等待队列长度
while(1)
{
printf("等待连接请求...\n");
len = sizeof(struct sockaddr);
ssock = accept(msock, (struct sockaddr *)&fsin, &len);
if(ssock == -1)
{
printf("accept出错");
exit(0);
}
if(send(ssock, s, strlen(s) + 1, 0) == SOCKET_ERROR)
{
printf("send出错");
exit(0);
}
strcpy(client_IP,inet_ntoa(fsin.sin_addr));
printf("收到客户端IP地址为:%s,端口号为:%d的连接请求\n",client_IP,fsin.sin_port);
printf("向客户端发出信息:%s\n",s);
closesocket(ssock);
}
return 0;
}
int passivesock(const char *service, const char *transport, int qlen)
{
struct servent *pse;
struct sockaddr_in sin;
int s, type;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
if ( pse = getservbyname(service, transport) )
sin.sin_port = htons(pse->s_port);
else if ((sin.sin_port=htons((unsigned short)atoi(service))) == 0)
exit(0);
if (strcmp(transport, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
if(( s = socket(PF_INET, type, 0))<0)
exit(0);
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
exit(0);
if (type == SOCK_STREAM && listen(s, qlen) < 0)
exit(0);
return s;
}
客户端程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <winsock.h>
#define WSVERS MAKEWORD(2,0)
#define LENGTH 1000
SOCKET connectsock(const char *host, const char *service, const char *transport);
int main(int argc, char *argvp[])
{
char *host = "127.0.0.1";
char *service = "8273";
char buf[1000];
SOCKET s;
WSADATA wsadata;
if(WSAStartup(WSVERS, &wsadata) != 0)
{
printf("WSAStartup 初始化失败");
exit(0);
}
printf("已向%s:%s发出了连接请求\n",host,service);
s = connectsock(host, service, "TCP");
printf("连接成功!\n");
recv(s,buf,LENGTH,0);
printf("收到从服务器发送来的信息:%s",buf);
getchar();
}
SOCKET connectsock(const char *host, const char *service, const char *transport)
{
struct hostent *phe;
struct servent * pse;
struct protoent *ppe;
struct sockaddr_in sin;
int s,type;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
if(pse = getservbyname(service, transport))
sin.sin_port = pse->s_port;
else if( (sin.sin_port = htons((u_short)atoi(service))) == 0)//htons是把整数转换成网络字节码,sin.sin_port = 20768;
{
exit(0);
}
if(phe = gethostbyname(host))
{
memcpy(&sin.sin_addr,phe->h_addr_list[0],phe->h_length);
}
else
{
exit(0);
}
if((ppe = getprotobyname(transport)) == 0)
{
exit(0);
}
if(strcmp(transport, "UDP") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
s = socket(PF_INET, type, ppe->p_proto);
if(s == INVALID_SOCKET)
{
printf("4");
exit(0);
};
if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("连接出错,不能连接到%s:%s,%d",host,service,GetLastError());
exit(0);
}
return s;
}
程序运行结果:
服务器端:
等待连接请求.。。。
收到客户端ip地址为127.0.0.1,端口号为:23303的连接请求
向客户端发出信息:+OK.THIS IS A TEST SERVICE
等待连接请求。。。
客户端:
已向127.0.0.1:8273发出了连接请求
连接成功!
收到从服务发送来的信息:+OK.THIS IS A TEST SERVICE