I have been trying to learn about sockets for the past day or so. I thought it would be a good idea to make a basic chat client and server to learn with, I have tried to make an asynchronous server so I don't need to use loads of threads etc and I have came into an issue I simply can't fix. When I start my server, it goes through all ok and waits at the point where it needs to wait for a connection. I then start up my makeshift 'client' that simply sends a string for now and my server crashes with a SocketException with the message
Additional information: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
I don't see how my socket is not connected when it has to accept the connection in the first place. I have been using this tutorial (https://msdn.microsoft.com/en-us/library/fx6588te(v=vs.110).aspx) as a guide and have looked at both my code and the tutorial and still don't understand what I am doing wrong, can anyone help me?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Chat_Application
{
class Server
{
private Socket serverSocket = null;
private volatile ArrayList connections = null; // will hold all client sockets
private const int port = 1090;
private IPAddress ipAddress = null;
private IPEndPoint ipEndPoint = null;
private Thread listenThread = null; // seperate thread to run the server
private ManualResetEvent allDone = null;
public Server()
{
this.serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.connections = new ArrayList();
ipAddress = IPAddress.Parse(GetLocalIPv4(NetworkInterfaceType.Ethernet));
ipEndPoint = new IPEndPoint(ipAddress, port);
listenThread = new Thread(StartListen);
allDone = new ManualResetEvent(false);
}
public void Start()
{
listenThread.Start();
}
public void StartListen()
{
this.serverSocket.Bind(ipEndPoint);
this.serverSocket.Listen(20);
Program.mainWin.console.Text += "\n<INFO> Socket bound, listening for connections...";
while (true)
{
allDone.Reset();
serverSocket.BeginAccept(new AsyncCallback(AcceptConnectionAsync), serverSocket);
Program.mainWin.console.Text += "\n<INFO> Conncetion accepted...";
allDone.WaitOne();
}
}
public void AcceptConnectionAsync(IAsyncResult AR)
{
Byte[] bufferBytes = new byte[1024];
allDone.Set();
Socket client = (Socket) AR.AsyncState;
int x = client.Receive(bufferBytes);
Program.mainWin.console.Text += System.Text.Encoding.Default.GetString(bufferBytes);
}
public string GetLocalIPv4(NetworkInterfaceType _type)
{
string output = "";
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
{
if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up)
{
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
output = ip.Address.ToString();
}
}
}
}
return output;
}
}
}