该办法主要是实现服务器端和客户端,实现双端的消息接受与发送
实现思路:
主要是在服务端和客户端分别创建两个进程
服务器端:一个进程发送数据,一个进程接受数据
客户端: 一个进程发送数据,一个进程接受数据
服务器端 server.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#define DEF_VAL 0
#define ERR_VAL -1
#define ERR_LOG(VAL) \
do \
{ \
perror(VAL); \
exit(ERR_VAL); \
} while (0)
int main(int argc, char const *argv[])
{
//创建流式套接字
int sockfd = socket(AF_INET, SOCK_STREAM, DEF_VAL);
if (sockfd == ERR_VAL)
{
ERR_LOG("socket");
}
printf("---------------------success!----------------------\n");
struct sockaddr_in saddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(atoi(argv[1])); // 把主机字节序换成网络字节序
saddr.sin_addr.s_addr = inet_addr("192.168.255.128"); // 把点分十进制的ip地址转换成网络字节序
// ip与套接子绑定
int ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
if (ret == ERR_VAL)
{
ERR_LOG("bind");
}
printf("-------------------bind success!-------------------\n");
// 把套接字设置成监听套接字,等待队列长度为10
ret = listen(sockfd, 10);
if (ret == ERR_VAL)
{
ERR_LOG("listen");
}
printf("------------------listen success!-------------------\n");
// 从等待队列取出连接请求,开始连接,产生通信套接字
char buf[1024] = {0};
int connfd = 0;
memset(buf, 0, sizeof(buf));
connfd = accept(sockfd, NULL, NULL);
if (connfd == ERR_VAL)
{
ERR_LOG("conn");
}
printf("------------------accept success!-------------------\n");
int pid = fork();
if (pid < 0)
{
ERR_LOG("fork");
}
else if (pid == 0)
{
while (1)
{
ret = read(connfd, buf, sizeof(buf));
if (ret <= 0)
{
printf("客户端异常退出!\n");
close(sockfd);
close(connfd);
exit(-1);
}
else
{
printf("read %d bytes : %s", ret, buf);
}
if (!strcmp(buf, "quit\n"))
break;
//write(connfd, buf, ret);
memset(buf, 0, sizeof(buf));
}
}
else
{
while (1)
{
if (!strcmp(buf, "quit\n"))
break;
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
write(connfd, buf, strlen(buf));
}
}
close(sockfd);
close(connfd);
return 0;
}
客户端 client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#define HOST_PORT 8888
#define HOST_IP "192.168.191.128"
#define DEF_VAL 0
#define ERR_VAL -1
#define ERR_LOG(VAL) \
do \
{ \
perror(VAL); \
exit(ERR_VAL); \
} while (0)
int main(int argc, char const *argv[])
{
int sockfd = socket(AF_INET, SOCK_STREAM, DEF_VAL);
if (sockfd == ERR_VAL)
{
ERR_LOG("socket");
}
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
int connfd = connect(sockfd, (struct sockaddr *)&server, sizeof(server));
if (connfd == ERR_VAL)
{
ERR_LOG("conn");
}
printf("----------------connection success!-----------------\n");
char buf[128] = {0};
int pid = fork(),ret;
if (pid < 0)
{
ERR_LOG("fork");
}
else if (pid == 0)
{
while (1)
{
ret = read(sockfd, buf, sizeof(buf));
if (ret <= 0)
{
printf("服务器端异常退出!\n");
close(sockfd);
exit(-1);
}
else {
printf("read %d bytes : %s", ret, buf);
}
if (!strcmp(buf, "quit\n"))
break;
//write(connfd, buf, ret);
memset(buf, 0, sizeof(buf));
}
}
else
{
while (1)
{
if (!strcmp(buf, "quit\n"))
break;
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
write(sockfd, buf, strlen(buf));
}
}
return 0;
}