Connecting PCs with Sockets - c#

I want to write a programm where multiple clients can join on a server. For now the clients are only able to ask for the servertime, which works perfectly fine, as long as the client and the server are on the same pc. I'm pretty sure that I have to change the EndPoint in the Connect() Method of the client, but I don't know what i should change it to.
Please help me to find a solution for this.
I have this code on my server:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Multiple_Clients
{
class Program
{
private static int port = 4567;
private static byte[] _buffer = new byte[1024];
private static List<Socket> _clientSockets = new List<Socket>();
private static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
Console.Title = "Server";
setupServer();
Console.ReadLine();
}
private static void setupServer()
{
Console.WriteLine("Setting up server...");
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, port));
_serverSocket.Listen(500);
_serverSocket.BeginAccept(new AsyncCallback(acceptCallback), null);
}
private static void acceptCallback(IAsyncResult AR)
{
Socket socket = _serverSocket.EndAccept(AR);
_clientSockets.Add(socket);
Console.WriteLine("Client connected");
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket);
_serverSocket.BeginAccept(new AsyncCallback(acceptCallback), null);
}
private static void receiveCallback(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
int received = socket.EndReceive(AR);
byte[] dataBuf = new byte[received];
Array.Copy(_buffer, dataBuf, received);
string text = Encoding.ASCII.GetString(dataBuf);
Console.WriteLine("Text received: " + text);
string response = string.Empty;
if (text.ToLower() != "get time")
{
response = "Invalid Request";
}
else
{
response = DateTime.Now.ToLongTimeString();
}
byte[] data = Encoding.ASCII.GetBytes(response);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(sendCallback), socket);
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket);
}
private static void sendCallback(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
socket.EndSend(AR);
}
}
}
And this on my client
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Multiple_Clients
{
class Program
{
private static int port = 4567;
private static Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
Console.Title = "Client";
connect();
sendLoop();
Console.ReadLine();
}
private static void connect()
{
int attempts = 0;
while(!_clientSocket.Connected)
{
try
{
attempts++;
_clientSocket.Connect(IPAddress.Loopback, port);
}
catch(SocketException)
{
Console.Clear();
Console.WriteLine("Connection attempts: " + attempts.ToString());
}
}
Console.Clear();
Console.WriteLine("Connected");
}
private static void sendLoop()
{
while (true)
{
Console.Write("Enter a request:");
string req = Console.ReadLine();
byte[] buffer = Encoding.ASCII.GetBytes(req);
_clientSocket.Send(buffer);
byte[] receivedBuf = new byte[1024];
int rec = _clientSocket.Receive(receivedBuf);
byte[] data = new byte[rec];
Array.Copy(receivedBuf, data, rec);
Console.WriteLine("Received: " + Encoding.ASCII.GetString(data));
}
}
}
}
Any suggestions on how to improve this question are welcome.
Thank you very much for helping me!

Your client allways connect to IPAddress.Loopback ... in fact the local IP Address 127.0.0.1. Exchange IPAddress.Loopback to the real IPAdress of your server, e. g. IPAddress.Parse("192.168.?.?") ...!

Related

Get Socket Server to listen for external Connections

I was wondering how i can manage that my Socket Server, that listens on port 3333 and for any incoming IP Addresses can listen for external connections. On my local network it all works fine, but if people try to connect to the server from another router they cant connect, i already added a Exception for my Server.exe in the Firewall and opened the Port 3333 on my router. Is there anything else i have to do?
Any help is appreciated
EDIT:
Server Code(C#):
private static Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static List<Socket> clientSockets = new List<Socket>();
private const int PORT = 3333;
private static byte[] buffer = new byte[100];
private static Random random = new Random();
private static int connectedClients = 0;
private void ReceiveCallback(IAsyncResult AR)
{
Socket current = (Socket)AR.AsyncState;
int received;
try
{
received = current.EndReceive(AR);
}
catch (SocketException)
{
Console.WriteLine("Client " + connectedClients + " Disconnected! (Forced)");
current.Close();
clientSockets.Remove(current);
return;
}
byte[] recBuf = new byte[100];
Array.Copy(buffer, recBuf, received);
string text = getText(Encoding.ASCII.GetString(recBuf));
if (text != "")
{
Console.WriteLine("Recieved: " + text);
}
if (current.Connected)
current.BeginReceive(buffer, 0, 100, SocketFlags.None, ReceiveCallback, current);
}
private void AcceptCallback(IAsyncResult AR)
{
Socket socket;
try
{
socket = serverSocket.EndAccept(AR);
}
catch (ObjectDisposedException)
{
return;
}
clientSockets.Add(socket);
socket.BeginReceive(buffer, 0, 100, SocketFlags.None, ReceiveCallback, socket);
connectedClients++;
Console.WriteLine("Client " + connectedClients + " Connected! [" + IPAddress.Parse(((IPEndPoint)socket.RemoteEndPoint).Address.ToString()) + "::" +
((IPEndPoint)socket.RemoteEndPoint).Port.ToString() + "]");
serverSocket.BeginAccept(AcceptCallback, null);
}
private void setupServer()
{
SocketPermission perm = new SocketPermission(NetworkAccess.Accept, TransportType.Tcp, ""/*Here would sit my IPv4*/, PORT);
perm.Assert();
serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT));
serverSocket.Listen(10);//0
Console.WriteLine("Server Started!");
Console.WriteLine("Listening for Clients...");
serverSocket.BeginAccept(AcceptCallback, null);
}
private void CloseAllSockets()
{
foreach (Socket socket in clientSockets)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
serverSocket.Close();
}
static void Main(string[] args)
{
Console.Title = "Server";
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Program prgrm = new Program();
prgrm.setupServer();
Console.ReadLine();
prgrm.CloseAllSockets();
}

Why I can send data from TCP Client to Server, but not vice versa?

I wrote an asynchronous TCP server-client in C#. Both programs run successfully when I test them in one computer (server and client run simultaneously in the same machine). But when tested in two different computers, server-client connection is established and the client is able to send data to server, but the server seems to be not sending anything to its client.
By using TCPView, I know that both server and client is receiving/sending data as expected. I thought it was firewall blocking them, I've disabled it and no use. What's wrong with my code?
Server:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
// Server code
namespace ChatConsole
{
class Program
{
static Socket serverSocket, clientSocket;
static byte[] buffer;
const int defaultBufferSize = 1024;
static void Main(string[] args)
{
try
{
Console.WriteLine("This is server.");
// Create a new server socket.
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Create IP End Point.
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 1001);
// Bind endpoint to socket.
serverSocket.Bind(ep);
// Start listening for incoming client.
serverSocket.Listen(4);
// Execute callback method when client request connection.
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
while (true) { }
}
catch(Exception ex)
{
Console.WriteLine("from Main(): " + ex.Message);
}
}
private static void AcceptCallback(IAsyncResult ar)
{
try
{
// Store client socket handle.
clientSocket = serverSocket.EndAccept(ar);
Console.WriteLine("Client {0} joined.", clientSocket.RemoteEndPoint);
// Welcome the client when connection established.
buffer = Encoding.ASCII.GetBytes("Hello from server!");
clientSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
// Begin receiving data from connected client.
buffer = new byte[1024];
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch (Exception ex)
{
Console.WriteLine("from AcceptCallback(): " + ex.Message);
}
}
private static void SendCallback(IAsyncResult ar)
{
// Terminate current send session.
clientSocket.EndSend(ar);
Console.WriteLine("Replied.");
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Store the length of received data.
int received = clientSocket.EndReceive(ar);
if (received == 0)
return;
// Convert the received data to string then print in the console.
Array.Resize<byte>(ref buffer, received);
string text = Encoding.ASCII.GetString(buffer);
Console.WriteLine(text);
Array.Resize<byte>(ref buffer, defaultBufferSize);
// Send back message from the client.
byte[] data = Encoding.ASCII.GetBytes("from server: " + text);
clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
catch (Exception ex)
{
Console.WriteLine("from ReceiveCallback(): " + ex.Message);
}
}
}
}
Client:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
// Client code
namespace ChatConsoleClient
{
class Program
{
static Socket clientSocket;
static IPEndPoint ep;
static byte[] buffer;
const int defaultBufferSize = 1024;
static void Main(string[] args)
{
try
{
Console.WriteLine("This is client.");
// Create new socket for client.
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Ask for server IP address.
string IP = Console.ReadLine();
// Create endpoint to be connected.
ep = new IPEndPoint(IPAddress.Parse(IP), 1001);
// Start connecting to the server.
clientSocket.BeginConnect(ep, new AsyncCallback(ConnectCallback), null);
buffer = new byte[1024];
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
while (true)
{
string text = Console.ReadLine();
byte[] data = Encoding.ASCII.GetBytes(text);
clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
}
}
catch (Exception ex)
{
Console.WriteLine("from Main(): " + ex.Message);
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
int received = clientSocket.EndReceive(ar);
if (received == 0)
return;
// Convert the received data to string then print in the console.
Array.Resize<byte>(ref buffer, received);
string text = Encoding.ASCII.GetString(buffer);
Console.WriteLine(text);
Array.Resize<byte>(ref buffer, defaultBufferSize);
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
private static void SendCallback(IAsyncResult ar)
{
clientSocket.EndSend(ar);
}
private static void ConnectCallback(IAsyncResult ar)
{
clientSocket.EndConnect(ar);
}
}
}
I have checked client and server running in two different systems but both of them having the same IP series and it works

C# SendData sends random times

I am trying to create a program where a server communicates to clients and give them commands about what to do. But when I connect multiple clients it sends the commands to the client multiple random times. My current code:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Server
{
class Program
{
private static byte[] _buffer = new byte[1024];
private static List<Socket> _clientSockets = new List<Socket>();
private static int SERVERPORT = 5555;
private static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static String command;
static void Main(string[] args)
{
Thread serverThread = new Thread(new ThreadStart(SetupServer));
Thread readThread = new Thread(new ThreadStart(checkInput));
serverThread.Start();
readThread.Start();
}
private static void SetupServer()
{
Console.WriteLine("Setting up server...");
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, SERVERPORT));
_serverSocket.Listen(100);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
private static void AcceptCallback(IAsyncResult AR)
{
while (true)
{
if (_serverSocket != null)
{
try
{
Socket socket = _serverSocket.EndAccept(AR);
_clientSockets.Add(socket);
Console.WriteLine("Client conntected");
}
catch
{
}
}
if (command != null)
{
byte[] data = Encoding.ASCII.GetBytes(command);
foreach (Socket s in _clientSockets)
{
s.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), s);
}
}
command = null;
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
}
private static void ReceiveCallback(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
//int received = socket.EndReceive(AR);
//byte[] dataBuf = new byte[received];
//Array.Copy(_buffer, dataBuf, received);
//string text = Encoding.ASCII.GetString(dataBuf);
string response = string.Empty;
response = Console.ReadLine();
byte[] data = Encoding.ASCII.GetBytes(response);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
}
private static void SendCallback(IAsyncResult AR)
{
try {
Socket socket = (Socket)AR.AsyncState;
socket.EndSend(AR);
}
catch {
}
}
public static void checkInput()
{
while (true)
{
command = Console.ReadLine();
}
}
}
}
An example. I start the server and start 3 clients next. This is the output:
I am totally clueless why it sends random times. Also why is there a double 2 on console #2. Thanks in advance!
This caused the problem:
if (command != null)
{
byte[] data = Encoding.ASCII.GetBytes(command);
foreach (Socket s in _clientSockets)
{
s.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), s);
}
}
command = null;
I added this piece of code to the readThread thread.
public static void checkInput()
{
while (true)
{
command = Console.ReadLine();
Console.WriteLine("Sending");
byte[] data = Encoding.ASCII.GetBytes(command);
foreach (Socket s in _clientSockets)
{
s.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), s);
}
command = null;
}
}
this solved it.

Cannot connect to server with external ip via socket.connect() in c#

I'm connecting to my server on the internal network. But I cannot connect to a server on an external network. The server's firewall on the external network closed. The port forwarding server was.
Server side codes:
private byte[] _buffer = new byte[1024];
private List<Socket> _clientSockets = new List<Socket>();
private Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public int PortNo;
private void SetupServer()
{
try
{
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, PortNo));
_serverSocket.Listen(1);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
}
catch(Exception hata)
{
MessageBox.Show(hata.Message);
}
}
private void AcceptCallBack(IAsyncResult AR)
{
Socket socket = _serverSocket.EndAccept(AR);
_clientSockets.Add(socket);
try
{
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
}
catch(Exception hata)
{
MessageBox.Show(hata.Message);
}
}
private void ReceiveCallBack(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
int received = socket.EndReceive(AR);
byte[] dataBuf = new byte[received];
Array.Copy(_buffer, dataBuf, received);
string text = Encoding.ASCII.GetString(dataBuf);
byte[] response = response = ekranGonderme();
try
{
socket.BeginSend(response, 0, response.Length, SocketFlags.None, new AsyncCallback(SendCallBack), null);
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
}
catch(Exception hata)
{
MessageBox.Show(hata.Message);
}
}
Client Side Codes:
private static Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int portNo = 20000;
IPAddress[] server_ip = Dns.GetHostAddresses(server's external ip address);
private void Form1_Load(object sender, EventArgs e)
{
LoopConnect();
LoopSendReceive()
}
private void LoopSendReceive()
{
try
{
byte[] buffer = Encoding.ASCII.GetBytes("ekran_iste");
_clientSocket.Send(buffer);
byte[] receiveBuf = new byte[999999];
int rec = _clientSocket.Receive(receiveBuf);
byte[] data = new byte[rec];
Array.Copy(receiveBuf, data, rec);
MemoryStream ms = new MemoryStream(data);
}
catch
{
}
}
private void LoopConnect()
{
int attempts = 0;
while (!_clientSocket.Connected)
{
try
{
attempts++;
_clientSocket.Connect(server_ip[0], portNo);
}
catch(SocketException)
{
}
}
}
}

C# Sockets and sending data to all clients

OK, so I am not very good at programming in general, but I want to try my hand at using sockets. To start I watched a Youtube video which I followed step by step, I got the finished product working 100% to the guides however I wish to modify it in order for the server to be able to send a message to all connected clients.
Here is the youtube video: Youtube Video - Sockets
So this is the code for the server class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
namespace Server
{
class Program
{
private static byte[] buffer = new byte[1024];
public static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public static List<Socket> clientSockets = new List<Socket>();
static void Main(string[] args)
{
Console.Title = "Server, " + clientSockets.Count.ToString() + " clients are connected";
SetupServer();
Console.ReadLine();
}
public static void SetupServer()
{
Console.WriteLine("Setting up server...");
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, 100));
_serverSocket.Listen(5);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
public static void AcceptCallback(IAsyncResult AR)
{
Socket socket = _serverSocket.EndAccept(AR);
clientSockets.Add(socket);
Console.WriteLine("Client Connected");
Console.Title = "Server, " + clientSockets.Count.ToString() + " clients are connected";
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallBack), socket);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
private static void RecieveCallBack(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
int received = socket.EndReceive(AR);
byte[] databuff = new byte[received];
Array.Copy(buffer, databuff, received);
string s = Encoding.ASCII.GetString(databuff);
Console.WriteLine("Text Received: " + s);
string response = string.Empty;
if (s.ToLower() == "get time")
{
response = DateTime.Now.ToLongTimeString();
}
else
{
response = "Invalid Request";
}
byte[] data = Encoding.ASCII.GetBytes(response);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(sendCallback), socket);
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallBack), socket);
}
private static void sendCallback(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
socket.EndSend(AR);
}
}
}
I had a pretty pathetic attempt at what I wanted to do:
private static void RecieveCallBack(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
int received = socket.EndReceive(AR);
byte[] databuff = new byte[received];
Array.Copy(buffer, databuff, received);
string s = Encoding.ASCII.GetString(databuff);
Console.WriteLine("Text Received: " + s);
foreach(Socket s1 in clientSockets){
string response = string.Empty;
if (s.ToLower() == "get time")
{
response = DateTime.Now.ToLongTimeString();
}
else
{
response = "Invalid Request";
}
byte[] data = Encoding.ASCII.GetBytes(response);
s1.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(sendCallback), s1);
s1.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallBack), s1);
}
}
wasn't really expecting it too work, but I gave it a go.
Another secondary question:
Is this the line of code which determines what IP and Port the server will be listening on?
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, 100));
Used the same code, had the same problem.
It is not working because the client console is stuck in readLine(), so it is not receiving responses. After you send a command don't call Console.ReadLine again.
To loop trough all clients and send a message:
private static void sentToAll(string s)
{
foreach (Socket socket in clientSockets)
{
byte[] data = Encoding.ASCII.GetBytes(s);
socket.Send(data);
}
}
private static IPEndPoint localEndPoint;
public static String IpAddress = "10.0.0.13";
public static int port = 3000;
localEndPoint = new IPEndPoint(IPAddress.Parse(IpAddress), port);
_serverSocket.Bind(localEndPoint);

Categories