一、概述
1、流 socket
2、报文 socket
二、UNIX socket
UNIX socket 用于同一主机系统上的应用程序之间通信。 UNIX socket核心:使用文件系统。
三、Internet socket
Internet socket 用于不同主机系统上的应用程序通信。 网络字节序:在网络传输中定义的字节序的定义。
四、测试
1、UNIX socket
1、server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#define SV_SOCK_PATH "/tmp/unix_test"
#define BUF_LEN 1024
int main(int argc, char* argv[])
{
char buf[BUF_LEN] = "";
int buf_len = 0;
struct sockaddr_un addr;
int cfd=0;
int sfd=0;
sfd = socket(AF_UNIX,SOCK_STREAM,0);
if(sfd == -1){
return -1;
}
if(remove(SV_SOCK_PATH)==-1 && errno!=ENOENT){
goto ERROTR_RETURN;
}
memset(&addr,0,sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path,SV_SOCK_PATH,sizeof(addr.sun_path)-1);
if(bind(sfd,(struct sockaddr*)&addr,sizeof(struct sockaddr_un)) == -1){
goto ERROTR_RETURN;
}
if(listen(sfd,5) == -1){
goto ERROTR_RETURN;
}
cfd = accept(sfd,NULL,NULL);
while(1){
memset(buf,0,BUF_LEN);
buf_len = 0;
buf_len = recv(cfd,buf,BUF_LEN,0);
if(buf_len == 0){
goto CLOSE_RETURN;
}
printf("recv data length is %d\n",buf_len);
printf("data: %s\n",buf);
}
return 0;
ERROTR_RETURN:
close(sfd);
return -1;
CLOSE_RETURN:
printf("clietn close!\n");
close(cfd);
close(sfd);
return 0;
}
2、client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define SV_SOCK_PATH "/tmp/unix_test"
int main(int argc, char* argv[])
{
struct sockaddr_un addr;
int cfd=0;
cfd = socket(AF_UNIX,SOCK_STREAM,0);
memset(&addr,0,sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path,SV_SOCK_PATH,sizeof(addr.sun_path)-1);
if(connect(cfd,(struct sockaddr*)&addr,sizeof(struct sockaddr_un)) == -1){
goto ERROTR_RETURN;
}
while(1){
send(cfd,"hello world!",12,0);
sleep(5);
}
return 0;
ERROTR_RETURN:
close(cfd);
return -1;
}
3、编译
onlylove@ubuntu:~/my/linux/unix_socket$ ls -l
total 8
-rw-rw-r-- 1 onlylove onlylove 772 Jun 5 19:35 client.c
-rw-rw-r-- 1 onlylove onlylove 1511 Jun 5 19:48 server.c
onlylove@ubuntu:~/my/linux/unix_socket$ gcc -o server server.c
onlylove@ubuntu:~/my/linux/unix_socket$ gcc -o client client.c
onlylove@ubuntu:~/my/linux/unix_socket$ ls -l
total 48
-rwxrwxr-x 1 onlylove onlylove 17016 Jun 5 19:53 client
-rw-rw-r-- 1 onlylove onlylove 772 Jun 5 19:35 client.c
-rwxrwxr-x 1 onlylove onlylove 17240 Jun 5 19:53 server
-rw-rw-r-- 1 onlylove onlylove 1511 Jun 5 19:48 server.c
onlylove@ubuntu:~/my/linux/unix_socket$
4、测试
2、Internet socket
1、server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#define BUF_LEN 1024
int main(int argc, char* argv[])
{
char buf[BUF_LEN] = "";
int buf_len = 0;
struct sockaddr_in addr;
int cfd=0;
int sfd=0;
sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd == -1){
return -1;
}
memset(&addr,0,sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
inet_pton(AF_INET,"127.0.0.1",&addr.sin_addr);
addr.sin_port = htons(8888);
bzero(&(addr.sin_zero),8);
if(bind(sfd,(struct sockaddr*)&addr,sizeof(struct sockaddr_in)) == -1){
goto ERROTR_RETURN;
}
if(listen(sfd,5) == -1){
goto ERROTR_RETURN;
}
cfd = accept(sfd,NULL,NULL);
while(1){
memset(buf,0,BUF_LEN);
buf_len = 0;
buf_len = recv(cfd,buf,BUF_LEN,0);
if(buf_len == 0){
goto CLOSE_RETURN;
}
printf("recv data length is %d\n",buf_len);
printf("data: %s\n",buf);
}
return 0;
ERROTR_RETURN:
close(sfd);
return -1;
CLOSE_RETURN:
printf("clietn close!\n");
close(cfd);
close(sfd);
return 0;
}
2、client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
int main(int argc, char* argv[])
{
struct sockaddr_in addr;
int cfd=0;
cfd = socket(AF_INET,SOCK_STREAM,0);
memset(&addr,0,sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
inet_pton(AF_INET,"127.0.0.1",&addr.sin_addr);
addr.sin_port = htons(8888);
bzero(&(addr.sin_zero),8);
if(connect(cfd,(struct sockaddr*)&addr,sizeof(struct sockaddr_in)) == -1){
goto ERROTR_RETURN;
}
while(1){
send(cfd,"hello world!",12,0);
sleep(5);
}
return 0;
ERROTR_RETURN:
close(cfd);
return -1;
}
3、编译
onlylove@ubuntu:~/my/linux/Internet_socket$ ls -l
total 8
-rw-rw-r-- 1 onlylove onlylove 830 Jun 5 21:01 client.c
-rw-rw-r-- 1 onlylove onlylove 1466 Jun 5 21:00 server.c
onlylove@ubuntu:~/my/linux/Internet_socket$ gcc -o server server.c
onlylove@ubuntu:~/my/linux/Internet_socket$ gcc -o client client.c
onlylove@ubuntu:~/my/linux/Internet_socket$ ls -l
total 48
-rwxrwxr-x 1 onlylove onlylove 17056 Jun 5 21:06 client
-rw-rw-r-- 1 onlylove onlylove 830 Jun 5 21:01 client.c
-rwxrwxr-x 1 onlylove onlylove 17184 Jun 5 21:06 server
-rw-rw-r-- 1 onlylove onlylove 1466 Jun 5 21:00 server.c
onlylove@ubuntu:~/my/linux/Internet_socket$
4、测试
|