网络通信Socket套接字简单了解

网络中进程之间如何通信?

本地进程间的通信(IPC)有很多方式

  • 消息传递(管道、FIFO、消息队列)
  • 同步(互斥量、条件变量、读写锁、文件和写记录锁、信号量)
  • 共享内存(匿名的和具名的)
  • 远程过程调用(Solaris门和Sun RPC)

什么是Socket?

socket起源于Unix,而Unix/Linux的基本哲学之一就是“一切皆文件”,都可以用“打开Open” —> “读写Read/Write” —> “关闭Close” 模式来操作。

理解:Socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读写I/O、打开、关闭)。

Socket的基本操作

既然Socket为“open-write/read/-close”模式的一种实现,那么socket就提供了这些操作相对应的函数接口。
以TCP为例。

socket()函数
1
int socket(int domain, int type, int protocol);
bind()函数
1
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
listen()、connect()函数
1
2
int listen(int sockfd, int backlog);
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
accept()函数
read()、write()函数等
close()函数

Socket中TCP的三次握手建立连接详解

三次握手

Socket中TCP的四次握手建立连接详解

在这里插入图片描述

一个例子

服务器端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

#define MAXLINE 4096

int main(int argc, char** argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[4096];
int n;

if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
printf("create socket error: %s(errno: %d)/n",strerror(errno),errno);
exit(0);
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(6666);

if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
printf("bind socket error: %s(errno: %d)/n",strerror(errno),errno);
exit(0);
}

if( listen(listenfd, 10) == -1){
printf("listen socket error: %s(errno: %d)/n",strerror(errno),errno);
exit(0);
}

printf("======waiting for client's request======/n");
while(1){
if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
continue;
}
n = recv(connfd, buff, MAXLINE, 0);
buff[n] = '/0';
printf("recv msg from client: %s/n", buff);
close(connfd);
}

close(listenfd);
}

客户端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

#define MAXLINE 4096

int main(int argc, char** argv)
{
int sockfd, n;
char recvline[4096], sendline[4096];
struct sockaddr_in servaddr;

if( argc != 2){
printf("usage: ./client <ipaddress>/n");
exit(0);
}

if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("create socket error: %s(errno: %d)/n", strerror(errno),errno);
exit(0);
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6666);
if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
printf("inet_pton error for %s/n",argv[1]);
exit(0);
}

if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
printf("connect error: %s(errno: %d)/n",strerror(errno),errno);
exit(0);
}

printf("send msg to server: /n");
fgets(sendline, 4096, stdin);
if( send(sockfd, sendline, strlen(sendline), 0) < 0)
{
printf("send msg error: %s(errno: %d)/n", strerror(errno), errno);
exit(0);
}

close(sockfd);
exit(0);
}