Linux下C语言socket编程

来源:互联网 发布:可以视频通话的软件 编辑:程序博客网 时间:2024/05/18 02:23

Server.c


#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>int main(void) {printf("Server is running \n");// 地址struct sockaddr_in sockAddr;    /** PROTO: int socket(int domain, int type, int protocol);* domain: AF_INET   ==> ipv5 *         AF_INET6  ==> ipv6*         AF_UNIX   ==> 本地套接字(使用一个文件)* type: SOCK_STREAM (可靠的面向服务或流套接字)*       SOCK_DGEAM  (数据报文服务或数据报文套接字)*       SOCK_SEQPACKET (可靠的连续数据包服务)*       SOCK_RAW (在网络层之上的原始协议)* protocol: 指定实际使用的传输协议。*           IPPORTO_TCP, IPPROTO_SCTP, IPPROTO_UDP, IPPROTO_DCCP 等*           如果是"0", 根据选定的domain和type选择使用缺省协议。* 如果发生错误,函数返回值为-1。 否则,函数会返回一个代表新分配的描述符的整数。*/ int socketFD = socket(AF_INET, SOCK_STREAM, 0);if (-1 == socketFD) {perror("Cannot create socket");exit(EXIT_FAILURE);}printf(" Create socket success! \n");memset(&sockAddr, 0, sizeof(struct sockaddr_in));sockAddr.sin_family = AF_INET;sockAddr.sin_port = htons(1100);sockAddr.sin_addr.s_addr = INADDR_ANY;int bind_res = bind(socketFD, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr_in));if (-1 == bind_res) {perror(" Error bind failed!");close(socketFD);exit(EXIT_FAILURE);}printf("Bind success ! \n");if (-1 == listen(socketFD, 10)){perror("Error Listen failed!");close(socketFD);exit(EXIT_FAILURE);}printf("Listen the port ... \n");for (;;) {int connectFD = accept(socketFD, NULL, NULL);if (0 > connectFD) {perror("Error Accept failed!");close(socketFD);exit(EXIT_FAILURE);}printf("Accept one client ... \n");char buf[15];ssize_t rval = recv(connectFD, buf, 15, 0);printf("Receive from client... \n");if (rval < 0) {perror("Error Receive failed!");shutdown(connectFD, SHUT_RDWR);close(connectFD);close(socketFD);exit(EXIT_FAILURE);} if (rval == 0) {printf("Ending connection \n");}else {printf("Received from client is: \n");printf("The buf is %s", buf);}shutdown(connectFD, SHUT_RDWR);close(connectFD);}close(socketFD);return 0;}


Client.c


#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>int main(void) {struct sockaddr_in sockAddr;int res;int socketFD = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if (-1 == socketFD) {perror ("Cannot create socket");exit(EXIT_FAILURE);}memset(&sockAddr, 0, sizeof(struct sockaddr_in));sockAddr.sin_family = AF_INET;sockAddr.sin_port = htons(1100);res = inet_pton(AF_INET, "192.168.0.101", &sockAddr.sin_addr);if (0 > res) {perror ("Error: first parameter is not a valid address family");close(socketFD);exit(EXIT_FAILURE);} else if (0 == res) {perror ("Error: Secend parameter does not contain valid ip");close(socketFD);exit(EXIT_FAILURE);}if (-1 == connect(socketFD, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr_in))) {perror("Error Cannot connect to server!");close(socketFD);exit(EXIT_FAILURE);}char buf[15] = "Hello World\n";if (0 > send(socketFD, buf, 15, 0)) {perror("Error Cannot send on stream socket");close(socketFD);exit(EXIT_FAILURE);}shutdown(socketFD, SHUT_RDWR);printf("Send data is: %s", buf);close(socketFD);return 0;}



原创粉丝点击