cliene send and recieve from server and vice versa

This is a discussion on cliene send and recieve from server and vice versa within the Networking/Device Communication forums, part of the General Programming Boards category; hello every body new problem i hope some one can help! first this is the code for sever: Code: #include ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    cliene send and recieve from server and vice versa

    hello every body
    new problem i hope some one can help!

    first this is the code for sever:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include<string.h>
    #include <netdb.h>
    #define SERVER_PORT 5432
    #define MAX_PENDING 1
    #define MAX_LINE 256
    
    // get sockaddr, IPv4 or IPv6:
    void *get_in_addr(struct sockaddr *sa)
    {
        if (sa->sa_family == AF_INET) {
            return &(((struct sockaddr_in*)sa)->sin_addr);
        }
    
        return &(((struct sockaddr_in6*)sa)->sin6_addr);
    }
    
    int
    main()
    {
    struct sockaddr_in sin;
    struct sockaddr_storage their_addr; // connector's address information
    char buf[MAX_LINE];
    int len;
    int mode;
    socklen_t sin_size;
    int s, new_s;
    char tt[INET6_ADDRSTRLEN];
    /* build address data structure */
    bzero((char *)&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_port = htons(SERVER_PORT);
    /* setup passive open */
    if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("simplex-talk: socket");
    return 1;
    }
    else
    printf("socket created \n");
    if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
    perror("simplex-talk: bind");
    return 1;
    }
    else
    printf("Bind made \n");
    listen(s, MAX_PENDING);
    /* wait for connection, then receive and print text */
    printf("server: waiting for connections...\n");
    while(1) {
    sin_size = sizeof their_addr;
    new_s = accept(s, (struct sockaddr *)&their_addr, &sin_size);
    if (new_s  < 0) {
    perror("simplex-talk: accept");
    continue;
    }
    else
    {
            inet_ntop(their_addr.ss_family,
                get_in_addr((struct sockaddr *)&their_addr),
                tt, sizeof tt);
            printf("server: got connection from %s\n", tt);
            printf("server is waiting message\n");
    }
    mode=0;
    while(1)
    {
    if (mode==0)
    { len = recv(new_s, buf, sizeof(buf), 0);
    printf(" Data Recieved : \n");
    fputs(buf, stdout);
    mode=1;}
    else
     {(fgets(buf, sizeof(buf), stdin)) ;
    buf[MAX_LINE-1] = '\0';
    len = strlen(buf) + 1;
    send(s, buf, len, 0);
    printf("buffer sent \n");
    mode=0;
    }
    
    }
    close(new_s);
    }
    
    
    return 0;
    }
    and the client
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <string.h>
    #define SERVER_PORT 5432
    #define MAX_LINE 256
    int
    main(int argc, char * argv[])
    {
    FILE *fp;
    struct hostent *hp;
    struct sockaddr_in sin;
    char *host;
    char buf[MAX_LINE];
    int s;
    char mode;
    int len;
    if (argc==2) {
    host = argv[1];
    }
    else {
    fprintf(stderr, "usage: simplex-talk host\n");
    return 1;
    }
    /* translate host name into peer’s IP address */
    hp = gethostbyname(host);
    if (!hp) {
    fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
    return 1;
    }
    /* build address data structure */
    bzero((char *)&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
    sin.sin_port = htons(SERVER_PORT);
    /* active open */
    if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("simplex-talk: socket");
    return 1;
    }
    else
    printf("socket created \n");
    if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    perror("simplex-talk: connect");
    close(s);
    return 1;
    }
    else
    printf("Connected \n");
    /* main loop: get and send lines of text */
    mode=1;
    while(1)
    {
    if (mode==0)
    { 
    
    len = recv(s, buf, sizeof(buf), 0);
    printf(" Data Recieved : \n");
    fputs(buf, stdout);
    mode=1;}
    else
     {(fgets(buf, sizeof(buf), stdin)) ;
    buf[MAX_LINE-1] = '\0';
    len = strlen(buf) + 1;
    send(s, buf, len, 0);
    printf("buffer sent \n");
    mode=0;
    }
    
    }
    
    return 0;
    }
    the problem is with the client which is when it recieve a socket from server
    how?
    is it by creating a new socket! means i have to accept the connection from the server? which means i am not a client any more(because client not accepting)?
    which means that if i want two terminals to send And recieve from each other both should be server? which means that such type of communication should not be between cleint server??

    who will help me with this deadlock?
    i hope that the question is clear and someone can help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    You posted too much code with crap indentation.
    SourceForge.net: Indentation - cpwiki
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to change lowercase to uppercase and vice-versa
    By Interista in forum C Programming
    Replies: 8
    Last Post: 11-03-2011, 04:56 PM
  2. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  3. int to char and vice versa
    By mekaj in forum C++ Programming
    Replies: 13
    Last Post: 12-12-2005, 11:35 AM
  4. Problem: far to near copy, and vice versa
    By aaronc in forum C Programming
    Replies: 1
    Last Post: 06-16-2004, 07:37 AM
  5. Binary to ascii and vice versa
    By thenrkst in forum C++ Programming
    Replies: 13
    Last Post: 03-30-2003, 01:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21