Knowledge about the detailed steps to establish server and associated socket socket I have already mentioned in python socket programming article too, we can see that the origin of a blog content socket connection socket programming, with due if the C related API so here incorporates a network socket programming content written in C language API correlation-based, the following specific implementation, debugging. Article link: http://www.cnblogs.com/uestc-mm/p/7296083.html
Server Server.c program content:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <string.h> #include <unistd.h> /************************************************************************************************************************ 1、int socket(int family,int type,int protocol) family: 指定使用的协议簇:AF_INET (IPv4) AF_INET6 (IPv6) AF_LOCAL (UNIX Protocol) AF_ROUTE (routing socket) AF_KEY (secret key socket) of the type: Use specified socket type: SOCK_STREAM (byte stream socket) SOCK_DGRAM Protocol: If the socket is a socket type is not the original, it is this parameter is 0 2, int the bind (int sockfd, struct the MYADDR the sockaddr * , int addrlen) sockfd: socket function returns the socket descriptor myaddr: is a pointer to the local IP address structure pointer myaddrlen: structural length struct the sockaddr { unsigned Short sa_family like; // type of communication protocol family AF_xx char sa_data [14]; @ 14 byte protocol address, the socket comprises an IP address and port number }; struct {the sockaddr_in Short int the sin_family; // type of communication protocol group unsigned short int sin_port; // port number struct in_addr sin_addr; // IP address unsigned char si_zero [8]; // 0 is filled with the length kept the same structure sockaddr }; . 3, Connect int (int sockfd, const struct the sockaddr * serv_addr, the socklen_t addrlen) sockfd: Socket function returns a socket descriptor serv_addr: Server IP address structure pointer addrlen: the length of the structure pointer 4, int listen (int sockfd, int backlog) sockfd: socket socket functions described Binds bind character backlog: setting the maximum number of connections can be connected to the client, when multiple client requests to the server, receive the impact of this value. The default value 20 is . 5, Accept int (int sockfd, struct cliaddr the sockaddr *, * the socklen_t addrlen) sockfd: Socket function after listen socket descriptor cliaddr: client socket interface address structure addrlen: Client Address structural length 6 , int the send (int sockfd, const void * msg, int len, int flags) 7, int recv (int sockfd, void * buf, int len, unsigned int flags) sockfd: socket function socket descriptor msg: pointer data is transmitted buf: storing the received data buffer len: length of the data, the flags is set to 0 ***************** ************************************************** ************************************************** *** * / int main ( int argc, char * the argv []) { int FD, new_fd, struct_len, numBytes, I; struct the sockaddr_in The server_addr; struct the sockaddr_in client_addr; char BUFF [BUFSIZ]; server_addr.sin_family = AF_INET; The server_addr .sin_port = htons ( 8000); server_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(server_addr.sin_zero), 8); struct_len = sizeof(struct sockaddr_in); fd = socket(AF_INET, SOCK_STREAM, 0); while(bind(fd, (struct sockaddr *)&server_addr, struct_len) == -1); printf("Bind Success!\n"); while(listen(fd, 10) == -1); printf("Listening....\n"); printf("Ready for Accept,Waitting...\n"); new_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len); printf("Get the Client.\n"); numbytes = send(new_fd,"Welcome to my server\n",21,0); while((numbytes = recv(new_fd, buff, BUFSIZ, 0)) > 0) { buff[numbytes] = '\0'; printf("%s\n",buff); if(send(new_fd,buff,numbytes,0)<0) { perror("write"); return 1; } } close(new_fd); close(fd); return 0; }
The client Client.c program content:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> int main(int argc,char *argv[]) { int sockfd,numbytes; char buf[BUFSIZ]; struct sockaddr_in their_addr; printf("break!"); while((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1); printf("We get the sockfd~\n"); their_addr.sin_family = AF_INET; their_addr.sin_port = htons(8000); their_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); bzero(&(their_addr.sin_zero), 8); while(connect(sockfd,(struct sockaddr*)&their_addr,sizeof(struct sockaddr)) == -1); printf("Get the Server~Cheers!\n"); numbytes = recv(sockfd, buf, BUFSIZ,0);//接收服务器端信息 buf[numbytes]='\0'; printf("%s",buf); while(1) { printf("Entersome thing:"); scanf("%s",buf); numbytes = send(sockfd, buf, strlen(buf), 0); numbytes=recv(sockfd,buf,BUFSIZ,0); buf[numbytes]='\0'; printf("received:%s\n",buf); } close(sockfd); return 0; }
Use gcc compiler client and server program is compiled and interpreted:
gcc -o Master Server.c
gcc -o Slave Client.c