socket编程连接

  Socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”。在许多操作系统中,套接字API最初是作为UNIX操作

系统的一部分而开发的,所以套接字API与系统的其他I/O设备集成在一起。应用程序要为因特网通信而创建一个套接字
(socket)时,操作系统就返回一个小整数作为描述符(descriptor)来标识这个套接字。然后应用程序以该描述符作为传递参
数,通过调用相应函数(如read、write、close等)来完成某种操作(如从套接字中读取或写入数据)。
在生活中,A要电话给B,A拨号,B听到电话铃声后提起电话,这时A和B就建立起了连接,A和B就可以讲话了。等交流结束,
挂断电话结束此次交谈。 打电话很简单解释了这工作原理:“open—write/read—close”模式。下面是网络socket通信的基本流程在这里插入图片描述
socket服务器和客户端示例代码
liu@192.168.203.134:~/liu/apue $ vim socket_server.c

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

#define LISTEN_PORT 8889
#define BACKLOG 13

int main(int argc, char **argv)
{
int rv = -1;
int listen_fd, client_fd = -1;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
socklen_t cliaddr_len;
char buf[1024];

listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if(listen_fd < 0 )
{
printf("create socket failure: %s\n", strerror(errno));
return -1;
}
printf("socket create fd[%d]\n", listen_fd);

memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(LISTEN_PORT);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if( bind(listen_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 )
{
printf("create socket failure: %s\n", strerror(errno));
return -2;
}
printf("socket[%d] bind on port[%d] for all IP address ok\n", listen_fd, LISTEN_PORT);

listen(listen_fd, BACKLOG);

while(1)
{
printf("\nStart waiting and accept new client connect...\n", listen_fd);
client_fd = accept(listen_fd, (struct sockaddr*)&cli_addr, &cliaddr_len);
if(client_fd < 0)
 {
 printf("accept new socket failure: %s\n", strerror(errno));
 return -2;
 }
printf("Accept new client[%s:%d] with fd [%d]\n", inet_ntoa(cli_addr.sin_addr),
ntohs(cli_addr.sin_port), client_fd);


 memset(buf, 0, sizeof(buf));
 if( (rv=read(client_fd, buf, sizeof(buf))) < 0)
{
printf("Read data from client socket[%d] failure: %s\n", client_fd, strerror(errno));
close(client_fd);
 continue;
}
else if( rv == 0 )
{
printf("client socket[%d] disconnected\n", client_fd);
 close(client_fd);
continue;
 }
printf("read %d bytes data from client[%d] and echo it back: '%s'\n", rv, client_fd, buf);

if( write(client_fd, buf, rv) < 0 )
{
 printf("Write %d bytes data back to client[%d] failure: %s\n", rv, client_fd,
strerror(errno));
 close(client_fd);
}

sleep(1);
close(client_fd);
}
close(listen_fd);
 }

liu@192.168.203.134:~/liu/apue $ vim socket_client.c

 1 /*********************************************************************************
 2 * Copyright: (C) 2020 liu IoT Studio
 3 * All rights reserved.
 4 *
 5 * Filename: socket_client.c
 6 * Description: This file is socket client sample source code, it will connect
 7 * socket server and send to/receive from it.
 8 * 
 9 * Version: 1.0.0(10/23/2018)
10 * Author: liu  
11 * ChangeLog: 1, Release initial version on "2021-1-3 01:38:08 PM"
12 * 
13 ********************************************************************************/
14 #include <stdio.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22
23 #define SERVER_IP "192.168.203.134"
24 #define SERVER_PORT 8889
25 #define MSG_STR "Hello World!"
26
27 int main(int argc, char **argv)
28 {
29 int conn_fd = -1;
30 int rv = -1;
31 char buf[1024];
32 struct sockaddr_in serv_addr;
33
34 conn_fd = socket(AF_INET, SOCK_STREAM, 0);
35 if(conn_fd < 0)
36 {
37 printf("create socket failure: %s\n", strerror(errno));
38 return -1;
39 }
40
41 memset(&serv_addr, 0, sizeof(serv_addr));
42 serv_addr.sin_family = AF_INET;
43 serv_addr.sin_port = htons(SERVER_PORT);
44 inet_aton( SERVER_IP, &serv_addr.sin_addr );
45
46 if( connect(conn_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
47 {
48 printf("connect to server [%s:%d] failure: %s\n", SERVER_IP, SERVER_PORT, strerror(errno));
49 return 0;
50 }
51
52 if( write(conn_fd, MSG_STR, strlen(MSG_STR)) < 0 )
53 {
54 printf("Write data to server [%s:%d] failure: %s\n", SERVER_IP, SERVER_PORT, strerror(errno));
55 goto cleanup;
56 }
57
58 memset(buf, 0, sizeof(buf));
59 rv = read(conn_fd, buf, sizeof(buf));
60 if(rv < 0)
61 {
62 printf("Read data from server failure: %s\n", strerror(errno));
63 goto cleanup;
64 }
65 else if( 0 == rv )
66 {
67 printf("Client connect to server get disconnected\n");
68 goto cleanup;
69 }
71 printf("Read %d bytes data from server: '%s'\n", rv, buf);
72
73
74 cleanup:
75 close(conn_fd);
76 }

在这里插入图片描述
学习socket编程的一个总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值