my socket
时间:2010-07-02 来源:checl1987_EE
server.c
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<errno.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define DEFAULTPORT 2014 #define DEFAULT_BACKLOG 20 int main(int argc ,char *argv[]) { struct sockaddr_in socket_addr; struct sockaddr_in socket_acpt; char buf[30] = {0}; int recv_len; int acpt_len = sizeof(struct sockaddr_in); int accept_clt;
memset(&socket_addr, 0, sizeof(socket_addr)); socket_addr.sin_family = AF_INET; socket_addr.sin_port = htons(DEFAULTPORT); socket_addr.sin_addr.s_addr = htons(INADDR_ANY); bzero(&(socket_addr.sin_zero), 8);
int socket_fd = socket(PF_INET, SOCK_STREAM, 0); if (socket_fd == -1){ printf("Socket Error log is : %s\n", strerror(errno)); close(socket_fd); exit(0); }
if (bind(socket_fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) == -1){ printf("Bind Error log is : %s\n", strerror(errno)); close(socket_fd); exit(0); } else { if (listen(socket_fd, DEFAULT_BACKLOG) == -1){ printf("Listen Error log is : %s\n", strerror(errno)); close(socket_fd); exit(0); } else { while(1){ accept_clt = accept(socket_fd, (struct sockaddr *)&socket_acpt, (socklen_t *)&acpt_len); if(accept_clt == -1){ printf("Accept Error log is : %s\n", strerror(errno)); close(socket_fd); exit(0); } printf("received a connection from %s\n", inet_ntoa(socket_acpt.sin_addr)); recv_len = recv(accept_clt, buf, sizeof(buf), MSG_WAITALL); if (recv_len == -1){ printf("Accept Error log is : %s\n", strerror(errno)); close(socket_fd); exit(0); } printf("recv data : %s\n", buf); memset(buf, 0, sizeof(buf)); } } } return 0; }
client.c
#include<stdio.h> #include<unistd.h> #include<string.h> #include<stdlib.h> #include<sys/socket.h> #include<errno.h> #include<netdb.h> #include<netinet/in.h> #include<arpa/inet.h>
#define DEFAULTPORT 2014 #define DEFAULT_BACKLOG 20 int main(int argc ,char *argv[]) { struct sockaddr_in socket_addr; struct hostent *host;
if (argc < 2) { fprintf(stderr,"Please enter what you want to send !\n"); exit(1); }
if((host=gethostbyname("127.0.0.1")) == NULL) { herror("gethostbyname error !"); exit(1); }
socket_addr.sin_family = AF_INET; socket_addr.sin_port = htons(DEFAULTPORT); socket_addr.sin_addr = *((struct in_addr *)host->h_addr); bzero(&(socket_addr.sin_zero), 8);
int socket_fd = socket(PF_INET, SOCK_STREAM, 0); if (socket_fd == -1) printf("Socket Error log is : %s\n", strerror(errno));
if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr)) == -1) { perror("connect error !"); } if (send(socket_fd, argv[1], 40, 0) == -1){ perror("send error !"); close(socket_fd); }
return 0; }
注意: 1 出现Transport endpoint is not connected错误,可能是由于send, recv时候,所选的socket_fd不对,应该选accept的返回的socketfd,这个新socketfd是真正的send,recv所用的socketfd,而不socket()出来的。
2 client地址配为127.0.0.1, server配为INADDR_ANY。
3 查看server启动起来没有,可以用命令netstat -na | grep 端口号(本例为2014)
#define DEFAULTPORT 2014 #define DEFAULT_BACKLOG 20 int main(int argc ,char *argv[]) { struct sockaddr_in socket_addr; struct sockaddr_in socket_acpt; char buf[30] = {0}; int recv_len; int acpt_len = sizeof(struct sockaddr_in); int accept_clt;
memset(&socket_addr, 0, sizeof(socket_addr)); socket_addr.sin_family = AF_INET; socket_addr.sin_port = htons(DEFAULTPORT); socket_addr.sin_addr.s_addr = htons(INADDR_ANY); bzero(&(socket_addr.sin_zero), 8);
int socket_fd = socket(PF_INET, SOCK_STREAM, 0); if (socket_fd == -1){ printf("Socket Error log is : %s\n", strerror(errno)); close(socket_fd); exit(0); }
if (bind(socket_fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) == -1){ printf("Bind Error log is : %s\n", strerror(errno)); close(socket_fd); exit(0); } else { if (listen(socket_fd, DEFAULT_BACKLOG) == -1){ printf("Listen Error log is : %s\n", strerror(errno)); close(socket_fd); exit(0); } else { while(1){ accept_clt = accept(socket_fd, (struct sockaddr *)&socket_acpt, (socklen_t *)&acpt_len); if(accept_clt == -1){ printf("Accept Error log is : %s\n", strerror(errno)); close(socket_fd); exit(0); } printf("received a connection from %s\n", inet_ntoa(socket_acpt.sin_addr)); recv_len = recv(accept_clt, buf, sizeof(buf), MSG_WAITALL); if (recv_len == -1){ printf("Accept Error log is : %s\n", strerror(errno)); close(socket_fd); exit(0); } printf("recv data : %s\n", buf); memset(buf, 0, sizeof(buf)); } } } return 0; }
client.c
#include<stdio.h> #include<unistd.h> #include<string.h> #include<stdlib.h> #include<sys/socket.h> #include<errno.h> #include<netdb.h> #include<netinet/in.h> #include<arpa/inet.h>
#define DEFAULTPORT 2014 #define DEFAULT_BACKLOG 20 int main(int argc ,char *argv[]) { struct sockaddr_in socket_addr; struct hostent *host;
if (argc < 2) { fprintf(stderr,"Please enter what you want to send !\n"); exit(1); }
if((host=gethostbyname("127.0.0.1")) == NULL) { herror("gethostbyname error !"); exit(1); }
socket_addr.sin_family = AF_INET; socket_addr.sin_port = htons(DEFAULTPORT); socket_addr.sin_addr = *((struct in_addr *)host->h_addr); bzero(&(socket_addr.sin_zero), 8);
int socket_fd = socket(PF_INET, SOCK_STREAM, 0); if (socket_fd == -1) printf("Socket Error log is : %s\n", strerror(errno));
if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr)) == -1) { perror("connect error !"); } if (send(socket_fd, argv[1], 40, 0) == -1){ perror("send error !"); close(socket_fd); }
return 0; }
注意: 1 出现Transport endpoint is not connected错误,可能是由于send, recv时候,所选的socket_fd不对,应该选accept的返回的socketfd,这个新socketfd是真正的send,recv所用的socketfd,而不socket()出来的。
2 client地址配为127.0.0.1, server配为INADDR_ANY。
3 查看server启动起来没有,可以用命令netstat -na | grep 端口号(本例为2014)
相关阅读 更多 +