linux 下socket编程 一对一阻塞通信

linux下socket编程,socket的流程和windows几乎一样,只是linux下不需要加载socket版本号。

1.create socket。

函数:int server_socket = socket(AF_INET, SOCK_STREAM, 0);

第一个参数:通信域    可选:AF_INET是IPV4, AF_INET6是IPV6

第二个参数: 通信类型 可选:SOCK_STREAM(面向连接可靠TCP),SOCK_DGRAM(无连接不可靠UDP)

第三个参数: 使用协议,和第二个参数对应,一般为0(依据第二个参数自动补充协议), IPPROTO_TCP(TCP), IPPROTO_UDP(UDP)

返回值为-1失败,否则成功

2.bind socket 

首先创建本地地址

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(12345);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    bzero(&(server_addr.sin_zero), 8);   //补齐

 int bind_result = bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));

返回值为-1则失败,否则成功

3.listen   

listen(server_socket, 5)  // 5为最多可以同时连接的客户端

返回值-1失败,否则成功

4.accept

struct sockaddr_in client_address;

socklen_t address_len;

int client_socket = accept(server_socket, (struct sockaddr *)&client_address, &address_len);

client_addres得到客户端地址

返回值大于0成功,否则失败

5.recv

recv(client_socket, recv_msg, BUFFER_SIZE, 0);

返回值大于0,表示接受到的字节长度;等于0,表示客户端退出;小于0,表示接收消息失败(如网络不好)

6.send

send(client_socket, input_msg, BUFFER_SIZE, 0);


服务器端代码:

#include<stdio.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>

int main(int argc, const char * argv[])
{
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(11332);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    bzero(&(server_addr.sin_zero), 8);

    int server_socket = socket(AF_INET, SOCK_STREAM, 0);
    if(server_socket == -1)
    {
	perror("socket error");
        return 1;
    }

    int bind_result = bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if(bind_result == -1)
    {
	perror("bind error");
	return -1;
    }

    if(listen(server_socket, 5) == -1)
    {
	perror("listen error");
	return -1;
    }

    struct sockaddr_in client_address;
    socklen_t address_len;
    int client_socket = accept(server_socket, (struct sockaddr *)&client_address, &address_len);
    if(client_socket == -1)
    {
	perror("accept error");
	return -1;
    }

    char recv_msg[1024];
    char reply_msg[1024];
    
    while(1)
    {
	bzero(recv_msg, 1024);
	bzero(reply_msg, 1024);

	printf("reply:");
	scanf("%s", reply_msg);
	send(client_socket, reply_msg, 1024, 0);

	long byte_num = recv(client_socket, recv_msg, 1024, 0);
	recv_msg[byte_num] = '\0';
	printf("client said:%s\n", recv_msg);
    }
    return 0;
}

客户端代码:

#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>

int main (int argc, const char * argv[])
{
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(11332);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    bzero(&(server_addr.sin_zero),8);

    int server_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (server_socket == -1) {
        perror("socket error");
        return 1;
    }
    char recv_msg[1024];
    char reply_msg[1024];

    if (connect(server_socket, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in))==0)     {
        while (1) {
            bzero(recv_msg, 1024);
            bzero(reply_msg, 1024);
            long byte_num = recv(server_socket,recv_msg,1024,0);
            recv_msg[byte_num] = '\0';
            printf("server said:%s\n",recv_msg);

            printf("reply:");
            scanf("%s",reply_msg);
            if (send(server_socket, reply_msg, 1024, 0) == -1) {
                perror("send error");
            }
        }

    }

    // insert code here...
    printf("Hello, World!\n");
    return 0;
}

由于是阻塞的,只能一问一答的通信。

你可能感兴趣的:(linux 下socket编程 一对一阻塞通信)