C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 03-25-2009, 09:28 AM   #1
Registered User
 
Join Date: Mar 2009
Posts: 35
Socket Destination Port Problem

Hi everybody,

I have an application where two different socket connections run in order to communicate with a machine. Over the first one (UDP) I send a request packet to ask for data and over the other one connection (TCP) I receive the data.
I repeat this procedure two times that is, I request a block of data and when it's finished I request a second one.By the first time it works without problems but by the second time I discovered with wireshark that the request packet is sent to another port (not over the original port). I know I haven't made changes between the first request call and the second one because I have a function where I initialize all connections at the beginning and it works for the first transmission.

Could the operative system change the destination port?

Can anyone help me?

Thank you very much
radeberger is offline   Reply With Quote
Old 03-25-2009, 10:57 AM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,321
no, OS will not change port for you

why not to use same connection?
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-25-2009, 12:19 PM   #3
Registered User
 
Join Date: Mar 2009
Posts: 35
Hi,

I must use different connections (design requirements...)
radeberger is offline   Reply With Quote
Old 03-25-2009, 12:59 PM   #4
Registered User
 
Join Date: Apr 2008
Posts: 282
try to post some code (a relevant minimal example), there is surely something that changes between the two requests.
root4 is offline   Reply With Quote
Old 03-26-2009, 01:40 AM   #5
Registered User
 
Join Date: Mar 2009
Posts: 35
Hi again,

Now I can describe the problem better

I have a tcp server that wait for data in the recv function after accept. I receive data buffer of 1500 bytes but I don't receive only one buffer. I receive more. The first time the socket receives something port=2000 (so it's right) but the second time and successively port=49154. Something is wrong with the receive function...

Code:
AcceptSocket = accept(socketInTCP, NULL, NULL);		 
		 if (AcceptSocket == INVALID_SOCKET)
		 {	 
			
			 printf("Server: WSA:\n");
			
		 }
		 else
		 {   
			 printf("Server: Client Connected!\n");
			 //Receive Packages from Client
			 while(run_tcpserver)
			 {  	bytesRecv = recv(AcceptSocket, recvbuf, sizeof(recvbuf), 0);
				if (bytesRecv > 0)
				{if(ctr1==1)
					{	
						WaitForSingleObject(mutex_req,INFINITE);
						//The first time it receives port = 2000
					}
				 
                                 //the second time and successively port =49154
                                 ctr1=0;
				 ofstream outfile(outputfile, ios::out |ios::ate | ios::binary);//open
				 rcv_data=rcv_data+bytesRecv;
				 cout << "\xd" << rcv_data;
				 
				 outfile.write (var, sizeof(var));
				 outfile.close();
				 memset(recvbuf,0,sizeof(recvbuf));
				 
                                 if(rcv_data==(length_dummy*4))
					{
						ReleaseMutex(mutex_req);
						
					}
have anyone any ideas??
radeberger is offline   Reply With Quote
Old 03-26-2009, 02:01 AM   #6
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,321
I do not see where you do anything with the recv_buf
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-26-2009, 02:14 AM   #7
Registered User
 
Join Date: Mar 2009
Posts: 35
I don't know. but the destination port is wrong after successively recv...

that is my question....
radeberger is offline   Reply With Quote
Old 03-26-2009, 02:24 AM   #8
Registered User
 
Join Date: Mar 2009
Posts: 35
now I have this code

Code:
while(AcceptSocket==SOCKET_ERROR)
{
   Acceptsocket=accept(socketInTCP,NULL;NULL)
}
socketInTCP=AcceptSocket
while(run_tcpserver)
		{  //comprobar length_dummy, abrir fichero, blucle, cerra fichero
			bytesRecv = recv(socketInTCP, recvbuf, sizeof(recvbuf), 0);
			if (bytesRecv > 0)
				{if(ctr1==1)
					{	
						
						p=ntohs(clientService.sin_port);
						cout << "port the first time it receives:" << p << "\n";
						m=0;
						
						
					}
					ctr1=0;
					port_array[m]=ntohs(clientService.sin_port);
					m++;

					ofstream outfile(outputfile, ios::out |ios::ate | ios::binary);//open
					rcv_data=rcv_data+bytesRecv;
					cout << "\xd" << rcv_data;
   					

				 outfile.write (var, sizeof(var));
				 outfile.close();
				 memset(recvbuf,0,sizeof(recvbuf));
				 if(rcv_data==(length_dummy*4))
					{
						
						ReleaseMutex(req_mutex);
					}
			 
			}
	}
the first time p=2000

and

the second time

port_array[0]=2000
but
port_array[1]=49154.....
radeberger is offline   Reply With Quote
Old 03-26-2009, 02:28 AM   #9
Registered User
 
Join Date: Mar 2009
Posts: 35
I don't understand why after the TCP connection the destination port of my UDP connection is changed...but I have made debugging and I know that it occurs...
radeberger is offline   Reply With Quote
Old 03-26-2009, 02:37 AM   #10
Registered User
 
Join Date: Mar 2009
Posts: 35
Sorry, I wrote something wrong,

the first time it receives port=2000

but the second time=49154 and not 2000
radeberger is offline   Reply With Quote
Old 03-26-2009, 02:39 AM   #11
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,321
Quote:
Originally Posted by radeberger View Post
I don't understand why after the TCP connection the destination port of my UDP connection is changed...but I have made debugging and I know that it occurs...
It seems somethere you have a memory overrun
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-26-2009, 03:15 AM   #12
Registered User
 
Join Date: Mar 2009
Posts: 35
I think it's a synchronization problem because I use a mutex for the screen and I think something is wrong but I can't see it....
radeberger is offline   Reply With Quote
Old 03-26-2009, 03:16 AM   #13
Registered User
 
Join Date: Mar 2009
Posts: 35
what would you recommend me to synchronize the cout on the screen? a mutex or a semaphore?
radeberger is offline   Reply With Quote
Old 03-26-2009, 03:21 AM   #14
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,321
Quote:
Originally Posted by radeberger View Post
what would you recommend me to synchronize the cout on the screen? a mutex or a semaphore?
what do you want to synchronize cout with?

If you are using MS multi-threaded library - cout is alrady synchronized
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-26-2009, 03:30 AM   #15
Registered User
 
Join Date: Mar 2009
Posts: 35
I don't want that two threads print out something on the screen at the same time
radeberger is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A question related to strcmp meili100 C++ Programming 6 07-07-2007 02:51 PM
Datagram Unix socket problem karas C Programming 9 07-27-2006 10:08 AM
Socket Help - Multiple Clients project95talon C Programming 5 11-17-2005 02:51 AM
sockets problem programming kavejska C Programming 0 07-25-2005 07:01 AM
Using Sockets to post to port WaterNut Windows Programming 3 04-15-2005 04:10 PM


All times are GMT -6. The time now is 06:33 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22