Socket Exception error - c#

I have a networking socket program in C#.net.
I have to connect with an ip: 169.254.74.65 and port:7998 and my ip is:169.254.74.63.
So I have this code:
using System;
using System.Net;
using System.Net.Sockets;
class MyTcpListener{
public static void Main(){
TcpListener server = null;
try{
Int32 port = 7998;
IPAddress localAddr = IPAddress.Parse("169.254.74.65");
server = new TcpListener(localAddr, port);
server.Start();
Byte[] bytes = new Byte[500];
String data = null;
while (true){
Console.Write("Waiting for a connection... ");
TcpClient client = server.AcceptTcpClient();
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0){
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
}
client.Close();}
}
catch (SocketException e){
Console.WriteLine("SocketException: {0}", e);}
finally{ server.Stop(); }
Ping works fine between two IPs. Even telnet 169.254.74.65 7998 gives me proper result and listens to the correct messages. So the connection is solid.
But when I run the above code it shows an exception:
> SocketException: System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start(Int32 backlog)
at System.Net.Sockets.TcpListener.Start()
at MyTcpListener.Main() in C:\Users\Administrator\source\repos\TCPListener\TCPListener\Program.cs:line 12
What is the problem here?

Your code works for me:
Have you actually hit your server at 127.0.0.1:7998 from a client socket code?
Update
So your Server IP is 169.xx.xx.65, while your own DEV machine IP is 169.xx.xx.63
Your code is something which creates a TCP Server connection. While (if I am not wrong) - you only need to connect to that HL7 machine.
Understand that the HL7 machine will be the server and your machine will be the client. So you just need TcpClient. Something like:
TcpClient client = new TcpClient("169.xx.xx.65", 7998);
Use Connect/GetStream etc methods per: https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient(v=vs.110).aspx

Related

Why is my tcp client unable to connect to my server

I am having trouble with my c# tcp code.
When I run the server and the client on the same computer, it will connect just fine.
But when I run the client on a different PC or on a phone, I get: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Here is the server code:
private static void CreateServer()
{
TcpListener server = null;
try
{
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
server.Start();
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
Console.Write("Waiting for a connection... ");
/*
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
var ipAddress = ipHostInfo.AddressList;
Console.WriteLine(ipHostInfo.HostName);
Console.WriteLine(ipAddress[0]);
*/
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
NetworkStream stream = client.GetStream();
int i;
try
{
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
}
}
catch(IOException e)
{
//Console.WriteLine(e);
Console.WriteLine("Restarting Server");
//client.Close();
//CreateServer();
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
//Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
I removed most of the comments. But that is the basic example for a tcp server from the documentation.
the client connect code is very simple:
tcpClient = new TcpClient();
tcpClient.Connect("192.168.0.7", 13000);
with the declaration for tcpClient saved in a less local spot for retaining and quickly reconnecting.
What I have tried:
I have made sure the IP address is correct, I even port forwarded and used my external IP, but got the same issue.
I made sure the firewall is not blocking the app on either device.
I tried using either device as the server.
I've looked up the problem and the only other person to have this issue needed to make sure he put in the correct IP and fix his firewall settings. I'm probably missing something super obvious.
One final piece of information, one device is wired to the router, I don't know if that is messing with anything.
One other thing, I tried:
tcpClient.Connect(new IPEndPoint(IPAddress.Parse("192.168.0.7"), 13000));
as well.
Whelp I was right. It was something very obvious and honestly dumb on my part.
on the server side:
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
does not result in the server listening on its ipv4, instead it listens on an ipv6
so what you want:
Remove the localAddr declaration, as it is not important, and instead of calling:
server = new TcpListener(localAddr, port);
call:
server = new TcpListener(IPAddress.Any, port);
IPAddress.Any is used to listen across all of the network interfaces the device has.

How to send data from C# to python using sockets?

I cannot find any example or a tutorial on how to send data from C# to python.
in my application, C# is supposed to keep reading data from a hardware and send it to python to be processed. i have tried to create a basic server on python and a basic client on C# and i was never able to establish connection between the client and the server with the following output from C# No connection could be made because the target machine actively refused it. i tested my python server on a python client and i was able to establish connection just fine.
how do i send data from C# to python correctly using sockets? is there any available tutorial on example i can follow? is there something wrong with my code? here it is:
Python Server code:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1234))
s.listen(5)
while True:
clientsocket, address = s.accept()
print(f"Connection from {address} has been established!")
clientsocket.send(bytes("Welcome to the server!", "utf-8"))
clientsocket.close()
C# Client Code:
static void ExecuteClient()
{
try
{
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 1234);
Socket sender = new Socket(ipAddr.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
try
{
sender.Connect(localEndPoint);
Console.WriteLine("Socket connected to -> {0} ",
sender.RemoteEndPoint.ToString());
byte[] messageReceived = new byte[1024];
int byteRecv = sender.Receive(messageReceived);
Console.WriteLine("Message from Server -> {0}",
Encoding.ASCII.GetString(messageReceived, 0, byteRecv));
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
// Manage of Socket's Exceptions
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
I know this question is old, but incase some else stumbles here in their internet searching.
I just started down the road of learning Python and I'm working on a similar situation with my Raspberry Pi (SERVER) and Windows PC (CLIENT).
The problem is you're making a call to get the hostname on each machine. This is will obviously be different. Your client needs to connect to the address of the server. Your client code is trying to connect to the machine it's running and that connection is being refused. The server is never contacted.
I made the following changes and was able to establish a connection.
Python Server code:
s.bind(("", 1234))
C# Client Code:
//IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
//IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("[SERVER IP]"), 1234);
Server Console Response:
Connection from ('[CLIENT IP]', 52074) has been established!
Client Console Response:
Socket connected to -> [::ffff:[SERVER IP]]:1234
Message from Server -> Welcome to the server!

Socket not being closed properly

Using the code as provided here.
When run consecutive times within 120 seconds, fails on my machine with a SocketException:
Only one usage of each socket address (protocol/network address/port) is normally permitted 127.0.0.1:24125
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
Windows 10 x64
Adding socket.Close(); after socket.Receive(receiveBuffer); does nothing. Perhaps obvious because Dispose() should call Close() automatically anyway.
There is no difference in behaviour if ported to F#. So it's to do with how the .NET libraries are being used rather than anything language specific (again, sorry if that's obvious).
The same occurs if the output .exe is run from Windows Command Prompt or from the debugger.
After socket is disposed in code, waiting longer than 120 seconds resets the socket and it can be used once more without error.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace tcpTestCSharp
{
class Program
{
static void Main(string[] args)
{
string response = "Hello";
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
if (ipAddress != null)
{
IPEndPoint serverEndPoint = new IPEndPoint(ipAddress, 24125);
byte[] receiveBuffer = new byte[100];
try
{
using (TcpClient client = new TcpClient(serverEndPoint))
{
using (Socket socket = client.Client)
{
socket.Connect(serverEndPoint);
byte[] data = Encoding.ASCII.GetBytes(response);
socket.Send(data, data.Length, SocketFlags.None);
socket.Receive(receiveBuffer);
Console.WriteLine(Encoding.ASCII.GetString(receiveBuffer));
}
}
}
catch (SocketException socketException)
{
Console.WriteLine("Socket Exception : ", socketException.Message);
throw;
}
Console.ReadLine();
}
}
}
}

In a project how to set up a "client" console application and a "server" console application

Alright so this might be worded wrong or using the wrong terminology. I want to know how I would set up a console application in my local machine that would be the "server" where it would run all my background tasks/events that happen on the client windows? I would only have one console application for the "server" and up to four "client" console applications.
Each of these would do separate things. The "server" application would just do all the calculations and functions and the client would just show in nice format what I want them to show from the results from the server.
Also I wouldn't know how to set it up in a C# type of project.
For a complete tutorial, I would recommend reading this Code Project article.
For some example code, the following client / server console applications are pulled from the MSDN.
Synchronous Client Socket Example:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SynchronousSocketClient {
public static void StartClient() {
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName())
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
// Create a TCP/IP socket.
Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );
// Connect the socket to the remote endpoint. Catch any errors.
try {
sender.Connect(remoteEP);
Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString());
// Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
// Send the data through the socket.
int bytesSent = sender.Send(msg);
// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test = {0}",
Encoding.ASCII.GetString(bytes,0,bytesRec));
// Release the socket.
sender.Shutdown(SocketShutdown.Both);
sender.Close();
} catch (ArgumentNullException ane) {
Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
} catch (SocketException se) {
Console.WriteLine("SocketException : {0}",se.ToString());
} catch (Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
} catch (Exception e) {
Console.WriteLine( e.ToString());
}
}
public static int Main(String[] args) {
StartClient();
return 0;
}
}
Synchronous Server Socket Example:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SynchronousSocketListener {
// Incoming data from the client.
public static string data = null;
public static void StartListening() {
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );
// Bind the socket to the local endpoint and
// listen for incoming connections.
try {
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true) {
Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket handler = listener.Accept();
data = null;
// An incoming connection needs to be processed.
while (true) {
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,0,bytesRec);
if (data.IndexOf("<EOF>") > -1) {
break;
}
}
// Show the data on the console.
Console.WriteLine( "Text received : {0}", data);
// Echo the data back to the client.
byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
public static int Main(String[] args) {
StartListening();
return 0;
}
}
To set this up, you would need to create a new solution in Visual Studio, and then add two console application projects to that solution, one for the client and one for the server. Once both projects are completed, you can copy and install the code provided above. Build the solution to generate a .exe file for both client and server. Locate your .exe files, run the server first, and then run the client second. You should see some output on both the server and client console windows.
EDIT: Please bear in mind that this will get you as far as running a server and client locally. When you distribute your server / client code to other machines, you will have to contend with firewalls, port forwarding, and potentially proxies depending on your network.

Listening to Port 5060

I developing an SIP client. For this I must listen to port 5060 for incoming SIP Server messages. For this I coded something. (Also I take admin rights in program.)
WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
if (hasAdministrativeRight == true)
{
TcpListener server;
Int32 port = 5060;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
server.Start();
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
Console.Write("Waiting for a connection... ");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
client.Close();
}
}
I get SocketException: "An attempt was made to access a socket in a way forbidden by its access permissions" (Native error code: 10013)...
Do you have a suggestion for this?
It seems that you were running two applications, and they are trying
to access the same socket.
What Microsoft says about your problem:
WSAEACCES (10013)
Translation: Permission denied
Description: An attempt was made
to access a socket in a way that is forbidden by its access
permissions. For example, this error occurs when a broadcast address
is used for sendto but the broadcast permission is not set by using
setsockopt(SO_BROADCAST).
Another possible reason for the WSAEACCES
error is that when the bind (Wsapiref_6vzm.asp) function is called (in
Microsoft Windows NT 4 .0 Service Pack 4 [SP4] or later), another
program, service, or kernel mode driver is bound to the same address
with exclusive access. Such exclusive access is a new feature of
Windows NT 4.0 SP4 and later, and it is implemented by using the
SO_EXCLUSIVEADDRUSE option.

Categories

Resources