Hi everyone!
could you please tell me what methods to use in order the clients be able to
send integers through socket to server and server can read them and display it on the screen ?
Thanks!
Hi everyone!
could you please tell me what methods to use in order the clients be able to
send integers through socket to server and server can read them and display it on the screen ?
Thanks!
1) There is no such thing as a "method" in C. Methods belong to OOP and C is not a OO language.
2) For socket programming check out :
Beej's Guide to Network Programming
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Code:/* Client Code - Client.c */ ----------------------------------- #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> void error(char *msg) { perror(msg); exit(0); } int main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; int x; if (argc < 3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } printf("Please enter the index: "); scanf("%d",&x); int tmp = htonl(x); n=write(sockfd, &tmp, 4); //n=write(sockfd, &x, sizeof(x)); if (n < 0) error("ERROR writing to socket"); return 0; } /* Server Code - Server.c */ ----------------------------------------- #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> void error(char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen; int y; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); // n = read(newsockfd,&y,sizeof(y)); if (n < 0) error("ERROR reading from socket"); int ii=ntohl(y); printf("Here is the message: %d\n",ii); return 0; } /* There is some problem with passing the integer via the socket. The server is working but it is not displaying the integer value send via socket by the client. Please help me and if possible rectify the code .. :) */
You've got the right idea. You don't have to worry about endianess for the int you send if both the server and the client are on little endian machines, which probably they are (it's just the networking layer that's big endian).
Also, you may want to use a type with a set, definite size, such as int32_t, which is guarranteed to be 4 bytes everywhere.
Last edited by MK27; 05-01-2010 at 04:40 PM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I have modified the server code but still it has not been able to read the integer via the socket. What changes are required in my code ? Do I need to make any changes in the client code also ? Kindly advice !!Code:/* Server Code - Server.c */ ----------------------------------------- #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> void error(char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen; int y; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); n = read(newsockfd,&y,sizeof(y)); if (n < 0) error("ERROR reading from socket"); int ii=ntohl(y); printf("Here is the message: %d\n",ii); return 0; }
Last edited by Salem; 05-02-2010 at 09:39 AM. Reason: Moved question outside code tags so Mk27 can see it ;)
Please can anyone help me ? Its urgent !! []
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Your client would seem to be missing a call to connect().
See Beej, he explains all.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.