服务器端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#define PORT 1234
#define MAXSIZE1024
static int clientProcess(int connfd, struct sockaddr_in clientSock)
{
int num = 0;
int i = 0;
char recvBuf[MAXSIZE];
char sendBuf[MAXSIZE];
char clientName[MAXSIZE];
num = recv(connfd, clientName, MAXSIZE, 0);
if (num < 0)
{
printf("recv clientName message from client!\n");
return -1;
}
clientName[strlen(clientName)-1] = '\0';
printf("You got's a connection from %s, client name is %s\n",
inet_ntoa(clientSock.sin_addr), clientName);
while (1)
{
num = recv(connfd, recvBuf, MAXSIZE, 0);
if (num == 0)
{
close(connfd);
return -1;
}
recvBuf[strlen(recvBuf)-1] = '\0';
printf("received client (%s) message :%s\n", clientName, recvBuf);
//将接收到的信息加密返回给客户端
for (i=0; i<num-1; i++)
{
if ((recvBuf[i]>='a' && recvBuf[i]<='z') || (recvBuf[i]>='A' && recvBuf[i]<='Z'))
{
recvBuf[i] = recvBuf[i] + 3;
if ((recvBuf[i]>'Z' && recvBuf[i]<'Z'+3) || (recvBuf[i]>'z'))
{
recvBuf[i] = recvBuf[i] -26;
}
}
sendBuf[i] = recvBuf[i];
}
sendBuf[num-1] = '\0';
send(connfd, sendBuf, MAXSIZE, 0);
}
close(connfd);
return 1;
}
int main(int argc, char **argv)
{
int listenfd;
int connectfd;
struct sockaddr_in serverSock;
struct sockaddr_in clientSock;
socklen_t len = 0;
pid_t pid;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0)
{
printf("Create socket fails!\n");
return -1;
}
int opt = SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
bzero(&serverSock, sizeof serverSock);
serverSock.sin_family = AF_INET;
serverSock.sin_port = htons(PORT);
serverSock.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listenfd, (struct sockaddr *)&serverSock, sizeof serverSock) < 0)
{
printf("bind fails!\n");
return -1;
}
if (listen(listenfd, 5) < 0)
{
printf("listen fails!\n");
return -1;
}
len = (socklen_t)(sizeof clientSock);
while (1)
{
connectfd = accept(listenfd, (struct sockaddr *)&clientSock, &len);
if (connectfd < 0)
{
printf("accept fails!\n");
return -1;
}
//创建子进程处理客户端的信息
if ((pid = fork()) > 0)
{
close(connectfd);
continue;
}
else if (pid == 0)
{
close(listenfd);
clientProcess(connectfd, clientSock);
return 1;
}
else
{
printf("fork() fails!\n");
return -1;
}
}
close(listenfd);
}
客户端代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT1234
#define MAXSIZE 1024
static int clientProcess(FILE *fp, int connfd)
{
char readLine[MAXSIZE];
char recvLine[MAXSIZE];
int num;
printf("connect to server success!\n");
printf("Input client's name:");
if (fgets(readLine, MAXSIZE, fp) == NULL)
{
printf("fgets fails!\n");
return -1;
}
send(connfd, readLine, MAXSIZE, 0);
while (fgets(readLine, MAXSIZE, fp) != NULL)
{
if (strncmp(readLine, "quit", strlen("quit")) == 0)
{
exit(-1);
}
send(connfd, readLine, strlen(readLine), 0);
num = recv(connfd, recvLine, MAXSIZE, 0);
if (num < 0)
{
printf("received fails!\n");
return -1;
}
recvLine[num-1] = '\0';
printf("Server message:%s\n", recvLine);
}
return 1;
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in serverSock;
struct hostent *he;
if (argc != 2)
{
printf("Usage: %s <IP Address>\n", argv[0]);
return -1;
}
he = gethostbyname(argv[1]);
if (he == NULL)
{
printf("gethostbyname fails!\n");
return -1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
printf("Create socket fails!\n");
return -1;
}
bzero(&serverSock, sizeof serverSock);
serverSock.sin_family = AF_INET;
serverSock.sin_port = htons(PORT);
serverSock.sin_addr = *((struct in_addr *)he->h_addr);
if (connect(sockfd, (struct sockaddr *)&serverSock, sizeof serverSock) < 0)
{
printf("connect fails!\n");
return -1;
}
clientProcess(stdin, sockfd);
close(sockfd);
return 1;
}
程序实现的功能很简单,代码写的也比较通俗易懂,基本上没什么注释!代码在我自己的虚拟机里面调试通过,编译即可运行!