linux环境socket服务端和客户端互相收发数据

61 阅读1分钟

客户端

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>

/** socket客户端和服务端互相收发 */

/* socket
 * bind
 * listen
 * accept
 * send/recv
 */

#define SERVER_PORT 8888
#define BACKLOG     10

int main(int argc, char **argv)
{
	int iSocketServer;
	int iSocketClient;
	struct sockaddr_in tSocketServerAddr;
	struct sockaddr_in tSocketClientAddr;
	int iRet;
	int iAddrLen;

	int iRecvLen;
	unsigned char ucRecvBuf[1000];

	int iClientNum = -1;

	signal(SIGCHLD,SIG_IGN);
	
	iSocketServer = socket(AF_INET, SOCK_STREAM, 0);
	if (-1 == iSocketServer)
	{
		printf("socket error!\n");
		return -1;
	}

	tSocketServerAddr.sin_family      = AF_INET;
	tSocketServerAddr.sin_port        = htons(SERVER_PORT);  /* host to net, short */
 	tSocketServerAddr.sin_addr.s_addr = INADDR_ANY; /** 本机所有IP */
	memset(tSocketServerAddr.sin_zero, 0, 8);
	
	iRet = bind(iSocketServer, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr));
	if (-1 == iRet)
	{
		printf("bind error!\n");
		return -1;
	}

	iRet = listen(iSocketServer, BACKLOG);
	if (-1 == iRet)
	{
		printf("listen error!\n");
		return -1;
	}

	while (1)
	{
		iAddrLen = sizeof(struct sockaddr);
		iSocketClient = accept(iSocketServer, (struct sockaddr *)&tSocketClientAddr, &iAddrLen);
		if (-1 != iSocketClient)
		{
			iClientNum++;
			printf("Get connect from client %d : %s\n",  iClientNum, inet_ntoa(tSocketClientAddr.sin_addr));
			if (!fork())
			{
				while (1)
				{
					/* 接收客户端发送的数据 */
					iRecvLen = recv(iSocketClient, ucRecvBuf, 999, 0);
					if (iRecvLen <= 0)
					{
						close(iSocketClient);
						return -1;
					}
					else
					{
						ucRecvBuf[iRecvLen] = '\0';
						printf("Get Msg From Client %d: %s\n", iClientNum, ucRecvBuf);

						/** 接收客户端发来的数据 */
						char *ucSendBuf = "hello world!!!";
						int iSendLen = send(iSocketClient, ucSendBuf, strlen(ucSendBuf), 0);
						if (iSendLen <= 0) {
							printf("回复失败\n");
							close(iSocketClient);
							return -1;
						}
					}
				}				
			}
		}
	}
	
	close(iSocketServer);
	return 0;
}

客户端

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
/** socket客户端和服务端互相收发 */
/* socket
 * connect
 * send/recv
 */
#define SERVER_PORT 8888

int main(int argc, char **argv) {
    int iSocketClient;
    int iRet;
    int iSendLen;
    unsigned char ucSendBuf[1000];

    struct sockaddr_in tSocketServerAddr;

    iSocketClient = socket(AF_INET, SOCK_STREAM, 0);

    tSocketServerAddr.sin_family = AF_INET;
    tSocketServerAddr.sin_port = htons(SERVER_PORT);

    if (argc != 2)
	{
		printf("Usage:\n");
		printf("%s <server_ip>\n", argv[0]);
		return -1;
	}

    if (0 == inet_aton(argv[1], &tSocketServerAddr.sin_addr)) {
        printf("connect error!\n");
        return -1;
    }
    memset(tSocketServerAddr.sin_zero, 0, 8);

    iRet = connect(iSocketClient, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr));
    if (iRet == -1) {
        printf("connect error!\n");
        return -1;
    }

    while (1) {
        if (fgets(ucSendBuf, 999, stdin)) {
            /** 发送数据给客户端 */
            iSendLen = send(iSocketClient, ucSendBuf, strlen(ucSendBuf), 0); 
            if (iSendLen <= 0) {
                close(iSocketClient);
                return -1;
            }
        }
        /** 接收服务端发来的数据 */
        int iRecvLen = recv(iSocketClient, ucSendBuf, sizeof(ucSendBuf), 0);
        if (iRecvLen > 0) {
            ucSendBuf[iRecvLen] = '\0';
            printf("server: %s\n", ucSendBuf);
        }
    }
    close(iSocketClient);
    return -1;
}