I'm trying to connect a server using socket, and i got this exeption:
System.Net.Sockets.SocketException: 'No connection could be made because the target machine actively refused it 127.0.0.1:49778'
this is the code:
private
static
bool
IsActive(
string
port)
Socket socket =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress iPAddress = IPAddress.Parse(
"
127.0.0.1"
);
IPEndPoint iPEndPoint =
new
IPEndPoint(iPAddress,
int
.Parse(port));
var
TryConnect = Task.Run(() => socket.Connect(iPEndPoint));
if
(TryConnect.Wait(TimeSpan.FromSeconds(
1
)))
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(
"
exit"
);
socket.Send(buffer,
0
, buffer.Length, SocketFlags.None);
return
true
;
}
catch
(Exception e) { }
return
false
;
connect to server:
static
void
Main(string[] args)
Socket socket =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Server_Session(socket);
if
(socket.Connected)
Listen(socket);
private
static
void
Server_Session(Socket socket)
IPAddress sreverIPAddress = IPAddress.Parse(
"
127.0.0.1"
);
IPEndPoint iPEndPoint =
new
IPEndPoint(sreverIPAddress,
8080
);
Console.WriteLine(
"
connecting..."
);
socket.Connect(iPEndPoint);
Console.WriteLine(
"
hello client : "
+ socket.LocalEndPoint);
string
msg =
"
client"
;
byte[] buffer = aSCII.GetBytes(msg);
socket.Send(buffer,
0
, buffer.Length, SocketFlags.None);
Upload_Conversation(socket);
if
(socket.Connected)
Listen(socket);
i don't know how to solve it... please help
What I have tried:
i don't even know what to do...
127.0.0.1 is your own machine, so the reason is almost certainly that (a) you haven't started a server which has invoked
listen
(TCP) or
recvfrom
(UDP) on port 49778, or (b) more likely, your firewall rejected the connection for security reasons, in which case you have to tell your firewall to allow it.
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.