网络编程服务器客户端模式
面向连接的套接字
一, 客户端
1, WSAStartup,这个函数必须用,是有关socket版本的函数。
2, 调用Socket()生成一个套接字号
int clientfd = socket(AF_INET, SOCK_STREAM, 0);
3, 用connect()函数来连接服务器端
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(HOST_PORT);
servaddr.sin_addr.S_un.S_addr = inet_addr("172.28.20.132");
//将client套接字与服务器地址相连
connect(clientfd,(const struct sockaddr *)&servaddr, sizeof(servaddr))
4, send()/recv(),向服务器端发送数据及接受数据
发送数据
send(clientfd,buf,strlen(buf) + 1, 0);
接受数据
while(1)
{
recv(clientfd,recvbuf,1400,0);
}
5, 关闭socket
closesocket(clientfd);
二, 服务器端
1,WSAStartup,这个函数必须用,是有关socket版本的函数
2,调用Socket()生成一个套接字号servfd
3,用bind()函数将servfd套接字和本地地址连在一起。
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(HOST_PORT);
servaddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
bind(servfd, (const struct sockaddr *)&servaddr, sizeof(servaddr))
4,监听,表示服务器已经准备好接受来自客户端的连接。
listen(servfd,10)
5,接收连接,其中clientfd是新产生的套接字号,用来和客户端进行通信。
for(;;)
{
clientfd = accept(servfd, (struct sockaddr *)&clientaddr,&length);
}
6,接收和发送数据
接收数据
while(1)
{
recv(clientfd,recvbuf,1400,0);
}
发送数据
send(clientfd,sendbuf,strlen(sendbuf) + 1, 0);
7,关闭socket
closesocket(sockfd);
closesocket(servfd);
客户端代码示例
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#define HOST_PORT 10000
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2,2);
//socket版本
if (WSAStartup( wVersionRequested, &wsaData ) != 0 )
return 0;
//产生client socket
int clientfd = socket(AF_INET, SOCK_STREAM, 0);
if(clientfd < 0)
{
printf("client create socket failed./n");
return 0;
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(HOST_PORT);
servaddr.sin_addr.S_un.S_addr = inet_addr("172.28.20.132");
char buf[BUFSIZ];
char recvbuf[BUFSIZ];
memset(buf,0,BUFSIZ);
memset(recvbuf,0,BUFSIZ);
strncpy(buf,"shawn's client",14);
//连接服务器地址
if(connect(clientfd,(const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("%d/n",errno);
return 0;
}
else
printf("connect server success!/n");
//数据的发送
for(int index = 0; index < 5; index++)
{
send(clientfd,buf,strlen(buf) + 1, 0);
_sleep(2);
}
strcpy(buf,"end");
send(clientfd,buf,strlen(buf) + 1, 0);
//数据的接收
while (1)
{
recv(clientfd,recvbuf,1400,0);
printf("%s/n",recvbuf);
if(strcmp(recvbuf,"end") == 0)
break;
}
//关闭socket
closesocket(clientfd);
getchar();
return 0;
}
服务器端代码示例
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define HOST_PORT 10000
#define MAX_PACKET_LENGTH 1400
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
//socket版本
wVersionRequested = MAKEWORD( 2, 2 );
if ( WSAStartup( wVersionRequested, &wsaData ) != 0 )
return 0;
int servfd, clientfd;
//创建server socket
servfd = socket(AF_INET, SOCK_STREAM, 0);
if(servfd < 0)
{
printf("server create socket failed./n");
return 0;
}
else
printf("server socket is success create!/n");
struct sockaddr_in clientaddr;
memset(&clientaddr,0,sizeof(clientaddr));
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(HOST_PORT);
servaddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
//将服务器socket套接字和本地地址绑定
if(bind(servfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
printf("bind failed./n");
return 0;
}
else
printf("server bind success!/n");
//监听,为accept做准备
if(listen(servfd,10) < 0)
{
printf("listen failed./n");
return 0;
}
else
printf("server is listen!/n");
char buf[MAX_PACKET_LENGTH];
char sendbuf[MAX_PACKET_LENGTH];
memset(buf,0,MAX_PACKET_LENGTH);
memset(sendbuf,0,MAX_PACKET_LENGTH);
strcpy(sendbuf,"hello nihao");
int length = sizeof(clientaddr);
for(;;)
{
//接收连接
clientfd = accept(servfd, (struct sockaddr *)&clientaddr,&length);
//数据的接收和发送
while(1)
{
recv(clientfd,buf,MAX_PACKET_LENGTH,0);
send(clientfd,sendbuf,strlen(sendbuf) + 1, 0);
printf("%s/n",buf);
if(strcmp(buf,"end") == 0)
{
strcpy(sendbuf,"end");
send(clientfd,sendbuf,strlen(sendbuf) + 1, 0);
break;
}
}
//关闭通讯socket
closesocket(clientfd);
}
//关闭服务器socket
closesocket(servfd);
return 0;
}