C# socket custom string - c#

I'm sending socket with this function:
public static List<Socket> samp = new List<Socket>();
IPHostEntry host = null;
Socket sock;
host = Dns.GetHostEntry(serverip);
foreach (IPAddress address in host.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, 7777);
sock = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ipe);
if (sock.Connected)
{
samp.Add(sock);
}
}
samp.Add(sock); sends my ip, for example 127.0.0.1, how can I add string to ip? For example: Nickname:127.0.0.1 ? Thanks in advance! Or how can I send two sockets? For example:
First socket - nickname
Second - ip
Thnx alot

Related

Connecting to C# socket through public IP address

Good evening everyone,
I'm having trouble connecting to a Socket using my IP address. I can connect by using my local address, but no luck getting it to work with my public one.
If I go on www.canyouseeme.org I can see that my server receives the connection and the site says it can see the service.
Here is the code. Client
public void Connect() {
IPAddress ipAddr = IPAddress.Parse("xx.xx.143.138");
clientSocket = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
// Connect
clientSocket.Connect(new IPEndPoint(ipAddr, 7777));
}
Server:
private void Listen() {
IPAddress ipAddr = IPAddress.Any;
serverSocket = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(ipAddr, 7777));
}
Thanks in advance,
Emiliano

Why won't my client connect to my server?

So I'm trying to make a simple chat server that sends an object, Message between them. My server creation/connection code is
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddr, port);
Socket serverSocket = new Socket(ipAddr.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
System.Console.WriteLine("Socket running on port " + port);
System.Console.WriteLine(ipHost);
serverSocket.Bind(localEndPoint);
System.Console.WriteLine("Accepting Connections");
serverSocket.Listen(10);
while (true)
{
Socket clientSocket = serverSocket.Accept();
Thread t = new Thread(() => Login.login(clientSocket));
System.Console.WriteLine("Client accepted.");
}
and my client creation/connection code is
System.Console.WriteLine("attempting to connect");
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Console.WriteLine("tesinft3");
clientSocket.Connect(IPAddress.Parse(server's external IP address), port);
System.Console.WriteLine("tesinft4");
System.Console.WriteLine("Connected to Server");
When I try to connect the client to the server I get attempting to connect and testinft3 and that's it. After about 30 seconds they appear again, but they still won't connect. How can I fix this?

Socket not connecting with IPEndPoint

I have a System.Net.Sockets.Socket connecting to a back end.
public class Connection
{
private readonly Socket _client;
private readonly IPEndPoint _endPoint;
byte[] _sendBuf = new byte[0x1000];
protected Connection(string host, int port)
{
var ipAddress = IPAddress.Parse(host);
_endPoint = new IPEndPoint(ipAddress, port);
_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
protected void Connect()
{
//Fails
_client.Connect(_endPoint);
}
}
The socket tries to connect but timeouts when Connect function is called. But if I provide host and port parameters to Socket.Connect function instead of an IPEndPoint with those, it connects successfully.
protected void Connect()
{
//Connects
_client.Connect(host, port); //same as used in creating IPEndPoint above
}
Why? The end point I'm trying to connect is in a VPN with the network I'm in.

C# Client-Server implementation error on client's socket "no connection could be made..."

I'm trying to launch my client, but have an error. Server is already running on the same computer. So I'm using "localhost" with GetHostEntry:
IPHostEntry ipHostInfo = System.Net.Dns.GetHostEntry("localhost");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, Port);
Sock = new Socket(remoteEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Sock.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), Sock);
But I have this "no connection could be made because the target machine actively refused it":
System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it [::1]:7777
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at NotifierClient.AsynchronousClient.ConnectCallback(IAsyncResult ar) in *** :line 156
Line 156 is
client.EndConnect(ar);
What is the reason? Could it be because ipHostInfo.AddressList[0] is an IPv6? How then I can accept Ipv4 address?
You can use the AddressFamily property of the IPAddress class to tell whether the address is IPv4 or IPv6.
This way you can loop through the list of IPAddress-es returned and choose the first one that is an IPv4 address:
IPHostEntry ipHostInfo = System.Net.Dns.GetHostEntry("localhost");
IPAddress ipAddress = null;
foreach(var addr in ipHostInfo.AddressList)
{
if(addr.AddressFamily == AddressFamily.InterNetwork) // this is IPv4
{
ipAddress = addr;
break;
}
}
// at this point, ipAddress is either going to be set to the first IPv4 address
// or it is going to be null if no IPv4 address was found in the list
if(ipAddress == null)
throw new Exception("Error finding an IPv4 address for localhost");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, Port);
Sock = new Socket(remoteEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Sock.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), Sock);
What I use for localhost for IPV4:
IPHostEntry ipHostInfo = Dns.GetHostEntry("localhost");
IPAddress ipAddress = ipHostInfo.AddressList[1];

How to get IP address with a host name?

I am making a client that connect to a server that is locally hosted that gets stock numbers from the server. The program does work if i use this code below but the way it works is by getting the dns name so in theory it only takes www.website.com and I cant figure out how I can get it to recognize a normal ip of 127.0.0.1 or localhost IP:
IPHostEntry ipHostInfo = Dns.GetHostEntry("www.website.com");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Attached is my attemp to get this to resolve the ip but I dont think I am approaching this right the full code can be seen here:StockReader Client Code
public class AsynchronousClient
{
private const int port = 21;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
// The response from the remote device.
private static String response = String.Empty;
private static void StartClient()
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
//******************ISSUE BEGINS HERE*********************************
string sHostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.GetHostEntry(sHostName);
IPAddress [] ipAddress = ipHostInfo.AddressList;
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client, "This is a test<EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
When you set the value of ipAddress, you can use the IPAddress.Parse method to pass in a string and retrieve an IPAddress object instead of using the domain name:
string ip = "127.0.0.1";
IPAddress address = IPAddress.Parse(ipAddress);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Try using string sHostName = "localhost";
// Establish the remote endpoint for the socket.
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, portnumber);
// Create a TCP/IP socket.
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(remoteEP);

Categories

Resources