socket programing error

Red Hat Enterprise Linux
Socket programming
/* CLIENT PROGRAM 1.6 */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #define MYPORT 2317 int main(int argc,char *argv[]) { int sockfd; struct sockaddr_in their_addr; struct hostent *he; int numbytes; if (argc < 3) { fprintf(stderr, "Client-Usage: %s <hostname> <message>n", argv[0]); exit(1); } if ((he = gethostbyname(argv[1])) == NULL) { perror("Client-gethostbyname() error lol!"); exit(1); } else printf("Client-gethostname() is OK...n"); if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Client-socket() error lol!"); exit(1); } else printf("Client-socket() sockfd is OK...n"); their_addr.sin_family = AF_INET; their_addr.sin_port = htons(MYPORT); their_addr.sin_addr = *((struct in_addr *)he->h_addr); memset(&(their_addr.sin_zero), '', 8); if((connect(sockfd,(struct sockaddr *)&their_addr,sizeof(their_addr))) < 0) { perror("Client-connect is error"); exit(1); } else printf("Client-connect is ok.."); if((numbytes = send(sockfd, argv[2], strlen(argv[2]), 0)) == -1) { perror("Client-sendto() error lol!"); exit(1); } else printf("Client-sendto() is OK...n"); printf("sent %d bytes to %sn", numbytes, inet_ntoa(their_addr.sin_addr)); if (close(sockfd) != 0) printf("Client-sockfd closing is failed!n"); else printf("Client-sockfd successfully closed!n"); return 0; } /********sever programing ************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define MYPORT 2317 #define MAXBUFLEN 500 int main(int argc, char *argv[]) { char buf[MAXBUFLEN]; int sockfd,newsockfd; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int addr_len, numbytes; memset(buf,0x00,sizeof(buf)); if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Server-socket() sockfd error lol!"); exit(1); } else printf("Server-socket() sockfd is OK...n"); my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = INADDR_ANY; memset(&(my_addr.sin_zero), '',if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("Server-bind() error lol!"); exit(1); } else printf("Server-bind() is OK...n"); if((listen(sockfd,10))== -1) { perror("Socket-listen error"); exit(1); } else printf("Socket-listen is ok.."); addr_len=sizeof(struct sockaddr); if(( newsockfd=accept(sockfd,(struct sockaddr*)&their_addr, &addr_len)) == -1) { perror("Server-accept is error"); exit(1); } else printf("Server-accept is ok..n"); 8); addr_len = sizeof(struct sockaddr); if(numbytes = recv(sockfd, buf, strlen(buf), 0) == -1) { perror("Server-recvfrom() error lol!"); exit(1); } else { printf("Server-Waiting and listening...n"); printf("Server-recvfrom() is OK...n%sn",buf); } printf("Server-Got packet from :%sn", inet_ntoa(their_addr.sin_addr)); printf("Server-Packet is %d bytes longn", numbytes); if(close(sockfd) != 0) printf("Server-sockfd closing failed!n"); else printf("Server-sockfd successfully closed!n"); return 0; } /****i got this error**/ Client-gethostname() is OK... Client-socket() sockfd is OK... Client-connect is error: Connection refused

Answer Wiki

Thanks. We'll let you know when a new response is added.

Its look like the server is not listening, could you provide the server runing output ? could you excecute the netstat command to see if the socket is listening ? (see your netstat reference of your operating system).


Discuss This Question:  

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: