Linux C语言实现的Socket通信
来源:互联网 发布:舵手软件视频教程 编辑:程序博客网 时间:2024/06/11 08:19
其实这篇文章就是前面一篇文章的复制体,主要是今天闲着无聊,就在Ubuntu下又写了一篇这个传说中的简单Socket通信。
以下是Linux网络编程的函数说明
'socket' Function
To perform network I/O, the first thing a process must do is call the socket function,
specifying the type of communication protocol desired (TCP using IPv4, UDP using
IPv6, Unix domain stream protocol, etc.).
#include <sys/socket.h>
int socket (int family, int type, int protocol);
Returns: non-negative descriptor if OK, -1 on error
具体参数如下
'connect' Function
The connect function is used by a TCP client to establish a connection with a TCP
server.
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);
Returns: 0 if OK, -1 on error
其中struct sockaddr
是一个结构体。
struct sockaddr_in { sa_family_t sin_family;/* address family: AF_INET */ in_port_t sin_port;/* port in network byte order */ struct in_addr sin_addr; /* internet address */};/* Internet address. */struct in_addr { uint32_t s_addr;/* address in network byte order */};
'bind' Function
#include <sys/socket.h>
int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);
Returns: 0 if OK,-1 on error
'listen' Function
#include <sys/socket.h>
#int listen (int sockfd, int backlog);
Returns: 0 if OK, -1 on error
'accept' Function
#include <sys/socket.h>
int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);
Returns: non-negative descriptor if OK, -1 on error
This function returns up to three values: an integer return code that is either a new
socket descriptor or an error indication, the protocol address of the client process
(through the cliaddr pointer), and the size of this address (through the addrlen
pointer). If we are not interested in having the protocol address of the client returned,
we set both cliaddr and addrlen to null pointers.
'close' Function
#include <unistd.h>
int close (int sockfd);
Returns: 0 if OK, -1 on error
服务器端:
#include<stdio.h>#include<sys/socket.h>#include<string.h>#include<sys/types.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<stdlib.h>#define MAXN 4096 int main(int argc ,char **argv){struct sockaddr_in A ,B ;char meg[MAXN] ;char rev[MAXN] ;socklen_t len;int s_socket,socket_conn;s_socket = socket(AF_INET,SOCK_STREAM,0);if(s_socket < 0){printf("Faile to socket!\n");return 1;}A.sin_family = AF_INET ;if(argv[1]){A.sin_port = htons(atoi(argv[1])) ;}elseA.sin_port = htons(1234);A.sin_addr.s_addr = htonl(INADDR_ANY) ;bind(s_socket ,(struct sockaddr *)&A,sizeof(A));listen(s_socket,5);printf("Server is Waiting ...\n");len = sizeof(struct sockaddr_in) ;socket_conn = accept(s_socket,(struct sockaddr *)&B ,&len);if(socket_conn >= 0){printf("连接成功,开始通信!\n");while(1){fgets(meg,MAXN,stdin);inet_ntoa(B.sin_addr);len = strlen(meg);if(meg[len-1] == '\n')meg[len-1] = 0 ;send(socket_conn,meg,strlen(meg)+1,0);printf("已经成功发送%d个字节\n",len);if(strcmp(meg,"quit") == 0){printf("Server is cancelling the communication!\n");break ;}recv(socket_conn,rev,MAXN,0);if(strcmp(rev,"quit")==0){printf("Client is cancelling the communication!\n");break ;}printf("成功接受%d个字符,字符为:%s\n",strlen(rev),rev);}close(socket_conn) ;}else{printf("Faile to aceept!\n");}close(s_socket);return 0;}
客户端:
#include<stdio.h>#include<string.h>#include<sys/socket.h>#include<sys/types.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<stdlib.h>#define MAXN 4096 int main(int argc, char **argv){int c_socket ,conn;int len ;char rev[MAXN] ,buf[MAXN];struct sockaddr_in A ;c_socket = socket(AF_INET,SOCK_STREAM,0);A.sin_family = AF_INET ;if(argv[1]){A.sin_port = htons(atoi(argv[1]));}if(argv[2]){A.sin_addr.s_addr = inet_addr(argv[2]);}conn = connect(c_socket,(struct sockaddr *)&A,sizeof(struct sockaddr_in));if(conn >= 0){printf("正在进行通信!\n");while(1){recv(c_socket,rev,MAXN,0);if(strcmp(rev,"quit") == 0){printf("Server is Cancelling the communication!\n");break ;} printf("成功接收到了%d字符,字符为:%s\n",strlen(rev),rev);fgets(buf,MAXN,stdin);len = strlen(buf) ;if(buf[len-1] == '\n')buf[len-1] = 0 ;send(c_socket,buf,strlen(buf)+1,0);if(strcmp(buf,"quit") == 0){printf("Client is Cancelling the communication!\n");break ;}}printf("Communication is Done!\n");}else{printf("Faile to connet\n");return 1;}close(c_socket);return 0;}
GCC编译过程:
gcc -Wal -o Server Server.c
./Server 1234
gcc -Wall -o Client Client.c
./Client 1234 127.0.0.1
- Linux C语言实现的Socket通信
- Linux下C语言实现的Socket通信
- C 语言 实现的socket 通信
- C 语言 实现的socket 通信
- c语言实现TCP的socket通信
- 用C语言实现的Socket通信
- C语言实现windows和linux下socket通信
- Linux环境下的c语言socket编程实现交互通信
- C语言实现服务器与客户端的socket通信运行在linux系统中
- C语言实现服务器与客户端的socket通信运行在linux系统中 .
- C语言--利用UDP实现socket通信
- C语言socket编程----实现TCP通信
- C语言Socket实现网络通信
- C语言实现Socket简单通信
- C语言-Socket通信
- C语言-Socket通信
- c语言socket通信
- c语言socket通信
- 关于正则表达式
- DDOS攻击相关问题
- MCR& MRC
- zjut 1280 小明A+B
- range
- Linux C语言实现的Socket通信
- httpd: Could not reliably determine the server's fully qualified domain name解决
- apache重启错误: httpd: Syntax error on line 57 of /etc/httpd/httpd.conf: Cannot load /usr/local/apache2/
- cookie 和session 的区别详解
- 坐标点排序问题
- 软件设计原则
- 子查询
- 推荐一些自我感觉比较好的java学习书
- 看看我这奇特的转换:无法识别的USB设备之超级神奇解决方案