LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2005, 09:43 AM   #1
swa_dur
LQ Newbie
 
Registered: Aug 2005
Posts: 1

Rep: Reputation: 0
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;

}
 
Old 08-29-2005, 11:30 AM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
What "incremental" value are you getting? Your code looks correct to me.

In the future, please post code in [ code ] tags, for easier readability.
 
Old 09-23-2005, 01:03 AM   #3
mccarl
LQ Newbie
 
Registered: Sep 2005
Location: Australia
Posts: 1

Rep: Reputation: 0
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
 
Old 09-23-2005, 11:03 AM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
socket pogramming(TCP) hubabuba Programming 4 10-24-2005 11:14 PM
TCP/IP book for socket programming hubabuba Programming 3 10-17-2005 03:46 AM
socket buffer TCP lucs Slackware 0 05-05-2005 07:26 AM
filetransfer with socket and tcp biiiep Programming 2 06-11-2004 12:33 PM
tcp socket monitoring xuttux Linux - Networking 1 05-19-2004 08:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 04:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration