Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-29-2005, 09:43 AM
|
#1
|
LQ Newbie
Registered: Aug 2005
Posts: 1
Rep:
|
Linux TCP Socket
hi all ,
i am working on TCP Socket Programming.
i need to Print the Port no on which the client is connected in Sever Program,
when i am Trying to Print the Port no which is a field of sockaddr_in structure, that is an argument of accept call in server.
but its not printing what we are passing in client.it is printing some incremental value.
the code looks as follows.
Server Program :
int main (int argc, char *argv[]) {
int sd, cliLen, optval,optlen,newSd;
struct sockaddr_in cliAddr, servAddr;
char line[MAX_MSG];
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket ");
return ERROR;
}
/* bind server port */
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(SERVER_PORT);
if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) {
perror("cannot bind port ");
return ERROR;
}
optval= 1;
optlen= sizeof(optval);
if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR , &optval, optlen) < 0)
printf("Cannot setsockopt SO_REUSEADDR\n");
listen(sd,5);
while(1)
{
printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT);
cliLen = sizeof(cliAddr);
newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
if(newSd<0)
{
perror("cannot accept connection ");
return ERROR;
}
printf("Acept Fd is %d\n",newSd);
/* init line */
memset(line,0x0,MAX_MSG);
/* receive segments */
while(read_line(newSd,line)!=ERROR)
{
printf("%s: received from %s:TCP%d : %s\n", argv[0], inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line);
/* init line */
memset(line,0x0,MAX_MSG);
} /* while(read_line) */
} /* while (1) */
}
here in the above code when we are printing the port value with the use of ntohs(cliAddr.sin_port)
the value what we are getting is some incremntal value.
can any one solve this problem
thanks a lottt
################################################
Client Program :
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close */
#define SERVER_PORT 1500
#define MAX_MSG 100
int main (int argc, char *argv[]) {
int sd, rc, i;
struct sockaddr_in localAddr, servAddr;
if(argc < 2) {
printf("usage: %s <data1> <data2> ... <dataN>\n",argv[0]);
exit(1);
}
/* h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s: unknown host '%s'\n",argv[0],argv[1]);
exit(1);
}
*/
bzero(&servAddr,sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr("3.209.134.204");
servAddr.sin_port = htons(20000);
printf("######### %u #########\n",ntohs(servAddr.sin_port));
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket ");
exit(1);
}
/* bind any port number */
/* localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
if(rc<0) {
printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT);
perror("error ");
exit(1);
}
*/
/* connect to server */
rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
if(rc<0) {
perror("cannot connect ");
exit(1);
}
for(i=1;i<argc;i++) {
rc = send(sd, argv[i], strlen(argv[i]) + 1, 0);
if(rc<0) {
perror("cannot send data ");
close(sd);
exit(1);
}
printf("%s: data%u sent (%s)\n",argv[0],i-1,argv[i]);
}
return 0;
}
|
|
|
08-29-2005, 11:30 AM
|
#2
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Rep: 
|
What "incremental" value are you getting? Your code looks correct to me.
In the future, please post code in [ code ] tags, for easier readability.
|
|
|
09-23-2005, 01:03 AM
|
#3
|
LQ Newbie
Registered: Sep 2005
Location: Australia
Posts: 1
Rep:
|
The client port that your are seeing is entirely correct. When the client connects it takes a random port to connect from. This is very often an incrementing port number every time you make a connection. If you want the client to connect "from" a specific port, than you should specify that in the call to bind from your client program. Otherwise, I am not sure exactly what port number you are expecting it to report!!
Cheers
Phil
|
|
|
09-23-2005, 11:03 AM
|
#4
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Rep: 
|
Quote:
Originally posted by mccarl
The client port that your are seeing is entirely correct. When the client connects it takes a random port to connect from. This is very often an incrementing port number every time you make a connection. If you want the client to connect "from" a specific port, than you should specify that in the call to bind from your client program. Otherwise, I am not sure exactly what port number you are expecting it to report!!
Cheers
Phil
|
On an aside, welcome to LinuxQuestions, Phil!
|
|
|
All times are GMT -5. The time now is 04:47 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|