socket通信过程都不了解的移步:socket通信过程及函数简介
话不多说,上代码
服务器端:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<errno.h>
#include<ctype.h>
#define BUF_SIZE 1024
#define LISTEN_COUNT 5
typedef struct sockaddr SOCKADDR;
typedef struct sockaddr_in SOCKADDR_IN;
int main(int argc, char *argv[])
{
int listenFd, connFd;
SOCKADDR_IN serverAddr, clientAddr;
socklen_t cliAddrLen = sizeof(clientAddr);
/* 创建监听套接字 */
if ((listenFd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket error");
exit(1);
}
/* 绑定IP和端口号 */
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(atoi(argv[1])); // argv[1]为端口号参数,atoi()将字符串转为整型,htons()将主机字节序舒徐转换为网络字节序
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); // INADDR_ANY代表本机所有IP
/* 解决bind:Address already in use */
int val = 1;
int ret = setsockopt(listenFd, SOL_SOCKET, SO_REUSEADDR, (void *)&val);
if (ret < 0) {
perror("Setsockopt error");
exit(1);
}
if (bind(listenFd, (SOCKADDR *)&serverAddr, sizeof(serverAddr)) < 0) {
perror("Bind error");
exit(1);
}
/* 监听是否有连接请求 */
if (listen(listenFd, LISTEN_COUNT) < 0) {
perror("Listen error");
exit(1);
}
printf("Listening...");
/* 收到连接请求 */
if ((connFd = accept(listenFd, (SOCKADDR *)&clientAddr, &clientAddrLen)) < 0) {
perror("Accept error");
exit(1);
}
printf("Accept connection request.");
while (1) {
char rdBuf[BUF_SIZE] = {0};
char wrBuf[BUF_SIZE] = {0};
/* 读取消息 */
int rdLen = read(connFd, rdBuf, BUF_SIZE);
if (rdLen < 0) {
perror("Read error");
break;
} else if (rdLen == 0) {
printf("The client has been disconnected!");
close(connFd);
break;
}
printf("Recv buffer:%s\n", rdBuf);
/* 将收到的消息转为大写 */
for (int i = 0; i < rdLen; ++i) {
wrBuf[i] = toupper(rdBuf[i]);
}
printf("Send buffer:%s\n", wrBuf);
write(connFd, wrBuf, rdLen);
}
close(listenFd);
return 0;
}
?客户端:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<errno.h>
#define BUF_SIZE 1024
typedef struct sockaddr SOCKADDR;
typedef struct sockaddr_in SOCKADDR_IN;
int main(int argc, char *argv[])
{
int connFd;
SOCKADDR_IN serverAddr;
/* 创建通信套接字 */
if ((connFd= socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket error");
exit(1);
}
/* 绑定IP和端口号 */
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(atoi(argv[2])); // argv[2]为端口号参数,atoi()将字符串转为整型,htons()将主机字节序舒徐转换为网络字节序
serverAddr.sin_addr.s_addr = inet_addr(argv[1]); // argv[1]为IP
connect(connFd, (SOCKADDR *)serverAddr, sizeof(serverAddr));
while (1) {
char rdBuf[BUF_SIZE] = {0};
char wrBuf[BUF_SIZE] = {0};
printf("Please enter data to send:");
fgets(wrBuf, BUF_SIZE, stdin)
write(connFd, wrBuf, BUF_SIZE);
/* 读取消息 */
int rdLen = read(connFd, rdBuf, BUF_SIZE);
if (rdLen < 0) {
perror("Read error");
break;
} else if (rdLen == 0) {
printf("The server has been disconnected!");
close(connFd);
break;
}
printf("Recv buffer:%s\n", rdBuf);
}
close(connFd);
return 0;
}
?编译命令:gcc server.c -o server
? ? ? ? ? ? ? ? ? ?gcc client.c -o client
?启动服务器:./server 4004
?启动客户端:./client 0.0.0.0 4004
?
?
?
|