服务端代码:
#include <stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include <unistd.h>
/*
socket
bind
listen
accept
send/receive
*/
#define SOCKET_PORT 8888
#define SOCKET_BACKLOG 10
int main(int argc, char **argv)
{
int iSocketSever;
struct sockaddr_in tSocketSeverAddress;
struct sockaddr_in tSocketClientAddress;
int iRet;
int iClientSocket;
int iAddrLen;
int iRecvLen;
unsigned char ucReceiveBuffer[1000];
int iClientNumber = -1;
iSocketSever = socket(AF_INET, SOCK_STREAM, 0);
if (iSocketSever < 0)
{
printf("socket error\n");
}
tSocketSeverAddress.sin_family = AF_INET;
tSocketSeverAddress.sin_port = htons(SOCKET_PORT);// host to network address,short port
tSocketSeverAddress.sin_addr.s_addr = INADDR_ANY;//本机所有IP
memset(tSocketSeverAddress.sin_zero , 0, 8);
iRet = bind(iSocketSever, ( struct sockaddr *)&tSocketSeverAddress, sizeof(struct sockaddr));
if (iRet == -1)
{
printf("bind error\n");
return -1;
}
iRet = listen(iSocketSever, SOCKET_BACKLOG);
if (iRet == -1)
{
printf("listen error\n");
return -1;
}
while (1)
{
iAddrLen = sizeof(struct sockaddr); //
iClientSocket = accept(iSocketSever, (struct sockaddr *)&tSocketClientAddress,&iAddrLen);
if (iClientSocket != -1)
{
iClientNumber ++;
printf("connect success,from client %d: %s\n", iClientNumber, inet_ntoa(tSocketClientAddress.sin_addr));
if (!fork())
{
/*子进程的源码*/
while (1)
{
/* 接受客户端发来的数据并显示出来 */
iRecvLen = recv(iClientSocket, ucReceiveBuffer,999,0);
if (iRecvLen <= 0)
{
close(iClientSocket);
return -1;
}
else
{
ucReceiveBuffer[iRecvLen+1] = '\0';
printf("get msg from client %d: %s\n", iClientNumber, ucReceiveBuffer);
}
}
}
}
}
close(iSocketSever);
return 0;
}
客户端代码:
#include <stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include <unistd.h>
/*
socket
bind
listen
accept
send/receive
*/
#define SOCKET_PORT 8888
#define SOCKET_BACKLOG 10
int main(int argc, char **argv)
{
int iSocketSever;
struct sockaddr_in tSocketSeverAddress;
struct sockaddr_in tSocketClientAddress;
int iRet;
int iClientSocket;
int iAddrLen;
int iRecvLen;
unsigned char ucReceiveBuffer[1000];
int iClientNumber = -1;
iSocketSever = socket(AF_INET, SOCK_STREAM, 0);
if (iSocketSever < 0)
{
printf("socket error\n");
}
tSocketSeverAddress.sin_family = AF_INET;
tSocketSeverAddress.sin_port = htons(SOCKET_PORT);// host to network address,short port
tSocketSeverAddress.sin_addr.s_addr = INADDR_ANY;//本机所有IP
memset(tSocketSeverAddress.sin_zero , 0, 8);
iRet = bind(iSocketSever, ( struct sockaddr *)&tSocketSeverAddress, sizeof(struct sockaddr));
if (iRet == -1)
{
printf("bind error\n");
return -1;
}
iRet = listen(iSocketSever, SOCKET_BACKLOG);
if (iRet == -1)
{
printf("listen error\n");
return -1;
}
while (1)
{
iAddrLen = sizeof(struct sockaddr); //
iClientSocket = accept(iSocketSever, (struct sockaddr *)&tSocketClientAddress,&iAddrLen);
if (iClientSocket != -1)
{
iClientNumber ++;
printf("connect success,from client %d: %s\n", iClientNumber, inet_ntoa(tSocketClientAddress.sin_addr));
if (!fork())
{
/*子进程的源码*/
while (1)
{
/* 接受客户端发来的数据并显示出来 */
iRecvLen = recv(iClientSocket, ucReceiveBuffer,999,0);
if (iRecvLen <= 0)
{
close(iClientSocket);
return -1;
}
else
{
ucReceiveBuffer[iRecvLen+1] = '\0';
printf("get msg from client %d: %s\n", iClientNumber, ucReceiveBuffer);
}
}
}
}
}
close(iSocketSever);
return 0;
}
运行结果: