Skip to main content

Thread: Socketing in C


guys, i'm running ubuntu 10.10.
i'm learning socket programming in c. i've written 2 simple codes.
serve , client.
i'm facing problems rum 'em. please guys, guide me in code i'm tired searching error last 3 hours.
want help.

server :

code:
#include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h>  int main(int argc, char **argv) {           int socketfd, portno, i, cli_len, n;     struct sockaddr_in serv_addr, cli_addr;     char buffer[256];      if (argc < 1) {         printf("args!\n");            return 2;         }     socketfd = socket(af_inet, sock_stream, 0);     if (socketfd < 0) {         printf("socket err\n");         return 2;     }     portno = atoi(argv[1]);     serv_addr.sin_family = af_inet;     serv_addr.sin_port = htons(portno);     serv_addr.sin_addr.s_addr = inaddr_any;      = bind(socketfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));     if( i<0 ) {         perror("error bind failed");         return 2;     }      if(-1 == listen(socketfd, 10)) {         perror("error listen failed");         return 2;     }     cli_len = sizeof(cli_addr);     printf("before accept\n");     int accept_fd = accept(socketfd, (struct sockaddr *)&cli_addr, &cli_len);     if (accept_fd < 0) {         perror("accept error");         return 2;     }      printf("connection successful!\n");     n = read(accept_fd,buffer,255);     if(n<0) printf("read err\n");     printf("here message : %s\n",buffer);         close(accept_fd);     close(socketfd);      return 0; }
and client here >>
code:
#include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <string.h> #include <stdio.h> #include <netdb.h>  int main(int argc, char *argv[]) {          struct sockaddr_in serv_addr;     struct hostent *server;     int portno, socketfd, n;     char buffer[256];      portno = atoi(argv[2]);     socketfd = socket(af_inet, sock_stream, 0);     if (socketfd < 0) {         perror("socket error!");     }     server = gethostbyname(argv[1]);     if (server == null) {         fprintf(stderr, " error\n ");         return 2;     }     memset((char *) &serv_addr, '0', sizeof(serv_addr));     serv_addr.sin_family = af_inet;     bcopy((char *)&server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server -> h_length);     serv_addr.sin_port = htons(portno);      printf("before connect\n");      n = connect(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));     if (n<0)  {         perror("connect error");         return 2;     }      printf("connection established\n");     memset((char *)&buffer, '0', sizeof(buffer));     printf("enter message!\n");     fgets(buffer,255,stdin);     n = write(socketfd, buffer, strlen(buffer));     if (n < 0) printf("write error!\n");     close(socketfd);     return 0; }

hope guys me possible.

quote posted rshinde70 view post
i'm facing problems rum 'em.
start describing problem. code compile? how compile it? run? how run it? error messages/what undesired behaviour observed?


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Socketing in C


Ubuntu

Comments