刚刚接触到服务器编程的知识,来温习一下,实现最基本的TCP服务器和客户端编程流程
#include
#include
int socket(int domain, int type, int protocol);
int bind(int sockfd, struct sockaddr *seraddr, int len);
struct sockaddr_in
{
sa_family_t sin_family; //协议族
u_int16_t sin_port; //端口号
struct in_addr sin_addr; //IP地址
};
struct in_addr
{
u_int32_t s_addr;
};
这里注意网络中字节序都是大端模式,一般都需要进行转化!
int listen(int sockfd, int size);
int accept(int sockfd, struct sockaddr *cliaddr, int *len);
int connect(int sockfd, (struct sockaddr *)&seraddr, int len);
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
int sockfd = socket(PF_INET, SOCK_STREAM, 0); //创建套接字
assert(sockfd != -1); //sockfd相当于描述符
struct sockaddr_in ser, cli;
memset(&ser, 0, sizeof(ser));
ser.sin_family = PF_INET; //协议族
ser.sin_port = htons(6000); //端口号
ser.sin_addr.s_addr = inet_addr("127.0.0.1"); //IP地址 //回环地址
int res = bind(sockfd, (struct sockaddr *)&ser, sizeof(ser)); //绑定
assert(res != -1);
listen(sockfd, 5); //启动监听
while(1) //服务器与客户端多次通讯
{
int len = sizeof(cli);
int c = accept(sockfd, (struct sockaddr *)&cli, &len); //返回本次连接到sockfd
if(c < 0)
continue;
while(1) //接受客户端发来的数据块
{
char recvbuf[128] = {0};
int n = recv(c, recvbuf, 127, 0);
if(n <= 0)
{
printf("one client break!\n");
break;
}
printf("%d %s\n",c, recvbuf);
send(c, "ok", 2, 0);
}
close(c); //关闭当前客户端的套接字
} //继续循环
close(sockfd); //关闭服务器
return 0;
}
#include
#include
#include
#include
#include
#include
#include
int main()
{
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
assert(sockfd != -1);
struct sockaddr_in ser,cli;
memset(&ser, 0, sizeof(ser));
ser.sin_famliy = PF_INET;
ser.sin_port = htons(6000); //端口号(与服务器匹配)
ser.sin_addr.s_addr = inet_addr("127.0.0.1"); //回环地址
int c = connect(sockfd, (struct sockaddr *)&ser, sizeof(ser)); //向服务器发送连接请求
assert(c != -1);
while(1)
{
char buf[128] = {0};
fgets(buf, 128, stdin);
buf[strlen(buf) - 1] = 0;
send(sockfd, buf, strlen(buf), 0); //发送到服务器
char recvbuf[128] = {0};
recv(sockfd, recvbuf, 127, 0);
printf("%s\n", recvbuf);
if(strcmp(buf, "end") == 0)
break;
}
close(c);
return 0;
}