![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 35
| Socket Destination Port Problem 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 | |
| | #2 |
| CSharpener 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 | |
| | #3 |
| Registered User Join Date: Mar 2009
Posts: 35
| Hi, I must use different connections (design requirements...) |
| radeberger is offline | |
| | #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 | |
| | #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);
}
|
| radeberger is offline | |
| | #6 |
| CSharpener 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 | |
| | #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 | |
| | #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);
}
}
}
and the second time port_array[0]=2000 but port_array[1]=49154..... |
| radeberger is offline | |
| | #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 | |
| | #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 | |
| | #11 |
| CSharpener Join Date: Oct 2006
Posts: 5,321
| 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 | |
| | #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 | |
| | #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 | |
| | #14 | |
| CSharpener Join Date: Oct 2006
Posts: 5,321
| Quote:
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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |