arijit_rebaca
asked on
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,
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,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :)
ASKER
I am working on RedHat Linux...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
pls tell me how do I implement "Wait for request on that connection" in the server section..... pls tell me as early as possible
ASKER
Hi,
I have done this.... But during running server it does not bind socket .....
error mgs :
Address already in use.
What is happening here???
I have done this.... But during running server it does not bind socket .....
error mgs :
Address already in use.
What is happening here???
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
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
ASKER
ya...
it is removed..
now another problem is that.... from client end it throughs the following error
Connect: Connection refused.
what is happening here....
it is removed..
now another problem is that.... from client end it throughs the following error
Connect: Connection refused.
what is happening here....
Did you connect to the correct server address and port ? Is there a firewall blocking access ?
ASKER
Server and client is running in the same m/c. and there is a firewall. Now what I will do next?
can you please show us the code?
regards
Manish Regmi
regards
Manish Regmi
ASKER
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");
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// ////
///////////////////////REC EIVED 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_a ddr));
#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,WN OHANG) > 0); /* clean up child processes */
//}
close(sockfd);
}
Tell me where is teh problem:
#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]
{
/* 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");
//////////////////////////
///////////////////////REC
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_a
#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
//////////////////////////
if ((numbytes=recv(new_fd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
//////////////////////////
//////////////////////////
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,WN
//}
close(sockfd);
}
Tell me where is teh problem:
In Client use AF_INET in socket call.
is the server running while you are executing your client code?
regards
Manish Regmi
is the server running while you are executing your client code?
regards
Manish Regmi
ASKER
ya server program is running at background
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
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
ASKER
i am passing my hostname i.e the m/c name
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
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
ASKER
I used
their_addr.sin_addr = inet_addr("127.0.0.1");
but it gives an error :
conversion to non-scalar type requested.
their_addr.sin_addr = inet_addr("127.0.0.1");
but it gives an error :
conversion to non-scalar type requested.
their_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
ASKER
Thanks a lot to all.... I have done at last...