服务器:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <netdb.h> #include <errno.h> #define PORT 2345 #define MAXSIZE 1024 int main(void) { int sockfd, newsockfd; int sin_size, portnumber; char buf[MAXSIZE] = {0}; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int client_addr_len = sizeof(struct sockaddr_in); //creat socket if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr, "creat socket failed "); exit(EXIT_FAILURE); } printf("create socket success! "); printf("sockfd is %d ", sockfd); bzero(&server_addr, sizeof(struct sockaddr_in)); server_addr.sin_family = AF_INET; //TCP/IP server_addr.sin_port = htons(PORT); //socket port server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //inet_addr("0.0.0.0") //bind sockfd and ip addr and port if (bind(sockfd, (struct sockaddr*)(&server_addr), sizeof(struct sockaddr)) < 0) { fprintf(stderr, "bind failed "); exit(EXIT_FAILURE); } printf("bind success! "); //listen sockfd, passive wait if (listen(sockfd, 10) < 0) { perror("listen fail "); } puts("start listen! "); //accept if((newsockfd = accept(sockfd, (struct sockaddr *)(&client_addr), &client_addr_len)) < 0) { perror("accept error"); exit(EXIT_FAILURE); } printf("accepted a new connection! "); printf("The new socket id is %d ", newsockfd); printf("Accept client ip is %s ", inet_ntoa(client_addr.sin_addr)); printf("Connet successful, please client input message "); //send message char sendbuf[MAXSIZE] = {0}; while (1) { int len = recv(newsockfd, buf, sizeof(buf), 0); if (strcmp(buf, "exit ") == 0) break; fputs(buf, stdout); send(newsockfd, buf, len, 0); memset(sendbuf, 0, sizeof(sendbuf)); memset(buf, 0 , sizeof(buf)); } close(newsockfd); close(sockfd); printf("exit success "); exit(EXIT_SUCCESS); return 0; }
客户端:
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <netdb.h> #include <errno.h> #define PORT 2345 #define MAXSIZE 1024 int main(void) { int sockfd; char buffer[1024] = {0}; struct sockaddr_in server_addr; struct hostent *host; int nbytes; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr, "Socket Error is %s ", strerror(errno)); exit(EXIT_FAILURE); } bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); if(connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) < 0) { fprintf(stderr, "Connect failed "); exit(EXIT_FAILURE); } char sendbuf[MAXSIZE] = {0}; char recvbuf[MAXSIZE] = {0}; while (1) { fgets(sendbuf, sizeof(sendbuf), stdin); send(sockfd, sendbuf, strlen(sendbuf), 0); if (strcmp(sendbuf, "exit ") == 0) break; recv(sockfd, recvbuf, sizeof(recvbuf), 0); fputs(recvbuf, stdout); memset(sendbuf, 0, sizeof(sendbuf)); memset(recvbuf, 0, sizeof(recvbuf)); } close(sockfd); exit(EXIT_SUCCESS); return 0; }