目录
服务端代码:
#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>
#include<errno.h>
/*
socket
bind
listen
accept
send/receive
*/
#define SOCKET_PORT 8888
#define SOCKET_BACKLOG 10
int main(int argc, char **argv)
{
char *errStr;
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)
{
errStr = (char*)strerror(errno);
printf("bind error, errno = %d\n",errno);
printf("bind error, error = %s\n",errStr);
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;
}
获取errno错误码:
函数原型:char *strerror(int errnum);
通过标准错误的编号errno,获得错误的描述字符串;
方便用户查找,返回值是指向错误信息的指针。
#include<errno.h>
#include<string.h>
char *errStr;
errStr = (char*)strerror(errno);
printf("bind error, error = %s\n",errStr);
客户端代码:
#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
connect
send/recva
*/
#define SOCKET_PORT 8888
int main(int argc, char **argv)
{
int iClientSocket;
struct sockaddr_in tSocketSeverAddress;
int iRet;
unsigned char ucSendBuffer[1000];
int iSendLen;
if (argc != 2)
{
printf("usage: \n");
printf("%s <server_ip>\n", argv[0]);
return -1;
}
iClientSocket = socket(AF_INET, SOCK_STREAM, 0);
tSocketSeverAddress.sin_family = AF_INET;
tSocketSeverAddress.sin_port = htons(SOCKET_PORT);
if ( inet_aton(argv[1],&tSocketSeverAddress.sin_addr) == 0 )
{
printf("invalid server IP \n");
return -1;
}
memset(tSocketSeverAddress.sin_zero , 0, 8);
iRet = connect(iClientSocket, (struct sockaddr *)&tSocketSeverAddress, sizeof(tSocketSeverAddress));
if ( iRet == -1 )
{
printf("connect failed\n");
}
while (1)
{
if (fgets(ucSendBuffer,999,stdin))
{
iSendLen = send(iClientSocket, &ucSendBuffer ,strlen(ucSendBuffer),0);
if ( iSendLen < 0)
{
close(iClientSocket);
return -1;
}
}
}
return 0;
}