-
socket programming error
hi
how can i fix error 10054 "An existing connection was forcibly closed by the remote host" ?
i write a program with c# , that should send a message to other pc with udp protocol , when I run the program , this error occurred !
I thank u , if u can solve this problem :)
-
Re: socket programming error
Sounds like the remote host is having a memory exception as a result of the message that it is receiving somehow. After you send this message what happens to the client on the remote host?
-
Re: socket programming error
this error is in the client side and stop it , server just runing and wait for client .
do u know why this problem has occurred ?
can this related to port number i used or cause of windos accessibility ... I don't know ! but I need too fix it !!
I can give the codes , if it is necessary.
-
Re: socket programming error
Most likely the client sends a message to the server, the server sends back a response, but the client can't handle the response data and dies.
This could be because the server response is not correct, or because the client code to handle the response is not correct.
from the MSDN:
Quote:
WSAECONNRESET 10054
An existing connection was forcibly closed by the remote host. This normally results if the peer application on the remote host is suddenly stopped, the host is rebooted, the host or remote network interface is disabled, or the remote host uses a hard close (see setsockopt for more information on the SO_LINGER option on the remote socket). This error may also result if a connection was broken due to keep-alive activity detecting a failure while one or more operations are in progress. Operations that were in progress fail with WSAENETRESET. Subsequent operations fail with WSAECONNRESET.
-
Re: socket programming error
Is the server/router set to allow UDP on the given port?
-
Re: socket programming error
AAaah this is a good point, is the router configured to allow comms on the port?
-
Re: socket programming error
just asking about allow the program to run , nothing about the port !
but i check the port i used , it's open !
-
Re: socket programming error
The port is important. Is the server you are connecting to written by you as well? Is it on the same LAN? Your message indicates that your attempt to send UDP was effectively booted by the receiver.
-
Re: socket programming error
Better check your firewall. Complaining about it won't help!
;)
-
Re: socket programming error
about the program and port , I use port number 9050 and it is open in my pc.
and i should say that I try another program ( with this purpose) , and it has a same problem! so i think this isn't related to codes.
about the LAN , yes they are on the same and I set right IP . ;)
and firewall , i turned it off ! (in "windows firewall with advanced security" / "windows firewall propertiese"/ turned off domain,private,public profiles)
-
Re: socket programming error
So both programs are running on the same pc ?
-
Re: socket programming error
yeah , try them on same pc.
about the ports , i have a question : is there a special port num for sending text message in udp ? or not , i can use any port , just open port ?!
-
Re: socket programming error
It's been a very long time since I have did anything at all with UDP. I use TCP for all communications now. I can't remember if there was any issues with running 2 UDP programs on the same PC or not.
There is no special port, just needs to be one that is available.
You still have not told us if the program you are sending to is a program written by you or someone else. If you have the code then it would be good to see what happens there when you send a message, for example does a received event fire? If the program was not written by you are you sure it accepts UDP transmissions? Have you tried it on a different PC from your client?
-
Re: socket programming error
Hmmm well #
1 just because both apps are on the same PC doesn't mean the firewall aint in the way. What address are you sending the packets too? Your IP or "127.0.0.1" or are you using a DNS function?
#2 Better post some source code, otherwise you get to hear all our fancy guesswork.
When you do post your code, put it in code tags.
It is an icon that looks like # between the <> icon and the quote icon.
Code:
void main (void)
{
//...
}
HTH,
-
Re: socket programming error
to DataMiser : no i don't write the program and about UDP transmission how can i know this ?!
yeah i should try it on other PC...
to ahoodin :
tnx for fancy guesswork ! i totally listen to them ;)
about the address , use 127.0.0.1
and i don't understand u , what's code tag ?!
-
Re: socket programming error
i think it's better to put the codes here and u see that !
how can i do that ? just copy and paste here ?
-
Re: socket programming error
I would think it would be documented if it accepts UDP transimissions.
Code tags can be typed as
[code]your code would go here[/code]
which would appear as The benift of the code tags is it allows the indenting to remain in the code and makes it much easier to read.
-
Re: socket programming error
client :
Code:
static void Main(string[] args)
{
byte[] data = new byte[1024];
string input, stringData;
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello !!";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
int recv = server.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
while (true)
{
input = Console.ReadLine();
if (input == "exit")
break;
server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
data = new byte[1024];
recv = server.ReceiveFrom(data, ref Remote);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("stop ! ");
server.Close();
}
}
server
Code:
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
Console.WriteLine("waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message recevied from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
string welcome = "Welcome !";
data = Encoding.ASCII.GetBytes(welcome);
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
while (true)
{
data = new byte[1024];
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
newsock.SendTo(data, recv, SocketFlags.None, Remote);
}
}
-
Re: socket programming error
Now I'm confused. You said that you did not write the server program but here you are showing code for it? So is this a different program or ? Is it still the same error? Are you getting anything at the server?
-
Re: socket programming error
why u confused !! I mean that i don't write it by myself ! this is a program and i want to test it but that error occured.
no , nothing i get !
-
Re: socket programming error
-
Re: socket programming error
The reason for asking the question was to see if you had the code or if you were attempting to talk to a program written by a 3rd party. Have you placed a break point and stepped through the code on the client or server? If not then this should be your first step when a program does not work as expected.
-
Re: socket programming error
I loaded your code and tried it The error you mention happens if you try to run the client with the server not already running. When the server is running the first attempt from the client did nothing it just hung waiting for input when none was there. I restarted the client and saw Hello show up on the server and then the client hung waiting for input.
Not sure what the goal is here but when it comes to reading data from a port be it TCP, UDP or whatever you need to make sure data is there before you try to read it otherwise you are going to hang on that line until data arrives and if it never arrives you are locked up.
-
Re: socket programming error
ok ... so what exactly should I do now !?
u mean that I run the client very soon , how u restarted client ... !
why this happen , u say hanging on lines !
-
Re: socket programming error
To get it to work I built the server and ran the server EXE first.
Then I went into the IDE and started the client in debug mode so i could see what it was doing.
The first time the client hung on a receive line [you can hit the pause button in the IDE and if a line of code is executing it will highlight the line]
I stopped the client then started it again [server is still running the whole time]
I see hello appear on the server window
Client hangs again this time on the next receive line.
You need to run your program in debug mode and step through the code to see what is happening. UDP is not a good way to send anything as there are no assurances that what you send will get to where you sent it. It is easier to use than TCP but TCP is far better. In either case you do not issue a read statement without first checking to see if there is data there to be read and you should have a way that the program can break out of the waiting for data state if no data appears in x amount of time.