Thread: Socket Destination Port Problem

  1. #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

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    no, OS will not change port for you

    why not to use same connection?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    35
    Hi,

    I must use different connections (design requirements...)

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    try to post some code (a relevant minimal example), there is surely something that changes between the two requests.

  5. #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??

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I do not see where you do anything with the recv_buf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #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....

  8. #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.....

  9. #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...

  10. #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

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #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....

  13. #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?

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

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