Solved

TCP/IP Client server programming

Posted on 2006-07-12
22
456 Views
Last Modified: 2012-08-13
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
Comment
Question by:arijit_rebaca
  • 10
  • 6
  • 3
  • +1
22 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 17097172
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 17097176
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
 

Author Comment

by:arijit_rebaca
ID: 17097182
I am working on RedHat Linux...
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 125 total points
ID: 17097196
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
 
LVL 8

Assisted Solution

by:manish_regmi
manish_regmi earned 50 total points
ID: 17097399
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
 

Author Comment

by:arijit_rebaca
ID: 17097816
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17097824
0
 

Author Comment

by:arijit_rebaca
ID: 17098218
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17098232
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
 

Author Comment

by:arijit_rebaca
ID: 17098390
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 17098845
Did you connect to the correct server address and port ? Is there a firewall blocking access ?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:arijit_rebaca
ID: 17105813
Server and client is running in the same m/c. and there is a firewall. Now what I will do next?
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17105827
can you please show us the code?

regards
Manish Regmi
0
 

Author Comment

by:arijit_rebaca
ID: 17105917
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
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17105964
In Client use AF_INET in socket call.

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

regards
Manish Regmi
0
 

Author Comment

by:arijit_rebaca
ID: 17106007
ya server program is running at background
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17106011
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
 

Author Comment

by:arijit_rebaca
ID: 17106064
i am passing my hostname i.e the m/c name
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17106119
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
 

Author Comment

by:arijit_rebaca
ID: 17106233
I used
their_addr.sin_addr = inet_addr("127.0.0.1");

but it gives an error :
conversion to non-scalar type requested.
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17106256
their_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
0
 

Author Comment

by:arijit_rebaca
ID: 17106960
Thanks a lot to all.... I have done at last...
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IDE C compiler 5 119
How to organize data in excel ? 2 106
SQL handling single and double quotes 3 88
Line meaning 9 77
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now