• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

TCP/IP Client server programming

Hi,

I want to write an application that is TCP server and client that full fill the following requirement :

TCP client request different name entry at different time and server should sent a list entry in reply depending upon the request name entry.

pls send me sample code as soon as possible.


regards,
0
arijit_rebaca
Asked:
arijit_rebaca
  • 10
  • 6
  • 3
  • +1
3 Solutions
 
Infinity08Commented:
0
 
Infinity08Commented:
Note that both these links contain a lot of example code that should get you started. If you have specific problems/questions, please post them here, but I don't think anyone here is gonna code this for you :)
0
 
arijit_rebacaAuthor Commented:
I am working on RedHat Linux...
0
Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

 
sunnycoderCommented:
The best guide for socket programming
http://beej.us/guide/bgnet/

This would give you the basic TCP client and server ...

Next all you need to do is instead of sending just any information, send something meaningful, parse it at the other end and respond appropriately ...

Flow would typically be

Server
Listen for connection
Accept and form a TCP connection
Wait for request on that connection
When request is received, parse it and process it and go back to waiting unless client requested terminating the connection


Client
Connect to server
send request, parse response ... repeat
request connection close.

You would get everything except request and response formation/parsing from the tutorial ... Give it a shot ... If you are stuck anywhere, post back here and we will help you out.

Cheers!
sunnycoder
0
 
manish_regmiCommented:
Any lessons will be incomplete without Richard stevens's book

Richard Stevens: Unix Network Programming
http://www.amazon.com/exec/obidos/tg/detail/-/013490012X?v=glance

the samples and source code from the book are here
http://www.kohala.com/start/unpv12e.html

regards
Manish Regmi

0
 
arijit_rebacaAuthor Commented:
thanks, I have satrted with ur help...

pls tell me how do I implement "Wait for request on that connection" in the server section..... pls tell me as early as possible
0
 
sunnycoderCommented:
0
 
arijit_rebacaAuthor Commented:
Hi,
I have done this.... But during running server it does not bind socket .....
error mgs :
Address already in use.

What is happening here???
0
 
sunnycoderCommented:
previous connection on that port has not been closed or it is a lingering socket ...
Make sure that you close the socket after you are done with it ... to use the same port, use SO_REUSEADDR socket option ...

man setsockopt
0
 
arijit_rebacaAuthor Commented:
ya...
it is removed..
now another problem is that.... from client end it throughs the following error

Connect: Connection refused.

what is happening here....
0
 
Infinity08Commented:
Did you connect to the correct server address and port ? Is there a firewall blocking access ?
0
 
arijit_rebacaAuthor Commented:
Server and client is running in the same m/c. and there is a firewall. Now what I will do next?
0
 
manish_regmiCommented:
can you please show us the code?

regards
Manish Regmi
0
 
arijit_rebacaAuthor Commented:
Here is my tcp client....
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORT 3440    /* the port client will be connecting to */

#define MAXDATASIZE 100 /* max number of bytes we can get at once */

int main(int argc, char *argv[])
{
    int sockfd, numbytes,new_fd;
    char buf[MAXDATASIZE];
    struct hostent *he;
    struct sockaddr_in their_addr; /* connector's address information */

    if (argc != 2)
    {
       fprintf(stderr,"usage: client hostname\n");
       exit(1);
    }

    if ((he=gethostbyname(argv[1])) == NULL)
    {
    /* get the host info */
       perror("gethostbyname");
       exit(1);
    }

    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    {
       perror("socket");
       exit(1);
    }

    memset(&(their_addr), 0, sizeof(their_addr));

    their_addr.sin_family = AF_INET;      /* host byte order */
    their_addr.sin_port = htons(PORT);    /* short, network byte order */
    their_addr.sin_addr = *((struct in_addr *)he->h_addr);
    printf("\n%s",he->h_addr);

    if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
    {

        perror("connecti");
        exit(1);
    }

////////////////// SEND Request to the Server for a particular type of Protocol (ex. RTP,HTTP)////////////////

    if (send(new_fd, "RTP\n", 4, 0) == -1)
        perror("send");

////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////RECEIVED the TEST_MODULE_LIST from Server////////////////////////////////////

    if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
    {
        perror("recv");
        exit(1);
    }


    buf[numbytes] = '\0';

    printf("Received Data:\n %s",buf);

///////////////////////////////////////////////////////////////////////////////////////////////////

    close(sockfd);

    return 0;
}

and tcp server:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>

#define MYPORT 3440    /* the port users will be connecting to */

#define BACKLOG 10     /* how many pending connections queue will hold */

#define MAXDATASIZE 100 /* max number of bytes we can get at once */

int main()
{
   int sockfd, new_fd;  /* listen on sock_fd, new connection on new_fd */
   int numbytes;
   struct sockaddr_in my_addr;    /* my address information */
   struct sockaddr_in their_addr; /* connector's address information */
   int sin_size;
   char buf[MAXDATASIZE];

   if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
   {
       perror("socket");
       exit(1);
   }

   memset(&(my_addr), 0, sizeof(my_addr));        /* zero the rest of the struct */
   my_addr.sin_family = AF_INET;         /* host byte order */
   my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
   my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */

   if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
   {
       perror("bind");
       exit(1);
   }

   if (listen(sockfd, BACKLOG) == -1)
   {
       perror("listen");
       exit(1);
   }

   //while(1)
   //{
     /* main accept() loop */
        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
        {
                perror("accept");
                exit(0);
                //continue;
        }
        printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));

#if 0
        if (!fork())
        {
          /* this is the child process */
                if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
                perror("send");
                close(new_fd);
                exit(0);
        }

#endif

////////////////////////////////////RECEIVED the Client Request and Parse it/////////////////////////////////////////

        if ((numbytes=recv(new_fd, buf, MAXDATASIZE, 0)) == -1)
        {
                perror("recv");
                exit(1);
        }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////PARSE The Received DATA from Client AND SEND RESPONSE//////////////////////////

        if(strcmp(buf,"RTP") == 0)
        {
                if (send(new_fd, "1. Regration Test\n2. Intregation Test\n3. Stress Test\n", 56, 0) == -1)
                perror("send");

        }
        else if(strcmp(buf,"HTTP\n") == 0)
        {
                if (send(new_fd, "1. Regration_Test\n2. Intregation_Test\n3. Stress_Test\n", 56, 0) == -1)
                perror("send");
        }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        close(new_fd);  /* parent doesn't need this */

    //while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
    //}
close(sockfd);

}

Tell me where is teh problem:
0
 
manish_regmiCommented:
In Client use AF_INET in socket call.

is the server running while you are executing your client code?

regards
Manish Regmi
0
 
arijit_rebacaAuthor Commented:
ya server program is running at background
0
 
manish_regmiCommented:
what are you passing as an argument.

since you are saying client and server is on same machine, what happens when you do
itheir_addr.sin_addr = inet_addr("127.0.0.1");

in the client code

regards
Manish Regmi

0
 
arijit_rebacaAuthor Commented:
i am passing my hostname i.e the m/c name
0
 
manish_regmiCommented:
The error is here:
their_addr.sin_addr = *((struct in_addr *)he->h_addr);

their_addr.sin_addr = inet_addr(*((struct in_addr *)he->h_addr));
or pass ip address and use inet_addr like my previous example

regards
Manish Regmi
0
 
arijit_rebacaAuthor Commented:
I used
their_addr.sin_addr = inet_addr("127.0.0.1");

but it gives an error :
conversion to non-scalar type requested.
0
 
manish_regmiCommented:
their_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
0
 
arijit_rebacaAuthor Commented:
Thanks a lot to all.... I have done at last...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell┬« is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 10
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now