Linux C語言實現的Socket通訊 分類: Linux 2011-12-19 00:24 630人閱讀 評論(0) 收藏 舉報socketlinuxc語言structdescriptor
其實這篇文章就是前面一篇文章的複製體,主要是今天閑著無聊,就在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
伺服器端:
[cpp] view plaincopyprint?
- #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])) ;
- }
- else
- A.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<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;}
用戶端:
[cpp] view plaincopyprint?
- #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;
- }
#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