I've been working on a chat for 2 people. everything is great (i have some exceptions to solve, but no big deal).
but the problem is, when i send a message from the client to server, then server to client, the server sends the last message it got from the client and only after you send again, it sends the right message and the other way around.
Client
private Socket _socket;
private byte[] _buffer;
private bool ok = false;
private ListBox _listbox;
public ClientSocket(ListBox list)
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_listbox = list;
}
public void Connect(string ipAddress, int port)
{
_socket.BeginConnect(new IPEndPoint(IPAddress.Parse(ipAddress), port), ConnectedCallback, null);
if(ok)
Item("Connected to the server!");
else Item("Could not Connect to the server!");
}
public void SendMessage(string text)
{
_buffer = new byte[1024];
_buffer = Encoding.Default.GetBytes(text);
_socket.BeginSend(_buffer, 0, _buffer.Length, SocketFlags.None, SentCallback, null);
}
private void ConnectedCallback(IAsyncResult result)
{
if (_socket.Connected)
{
_socket.EndConnect(result);
_buffer = new byte[1024];
_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, null);
ok = true;
}
}
private void ReceivedCallback(IAsyncResult result)
{
//_buffer = new byte[1024];
int bufLength = _socket.EndReceive(result);
//byte[] packet = new byte[bufLength];
//Array.Copy(_buffer, packet, packet.Length);
Array.Resize(ref _buffer, bufLength);
Item(PacketHandler.Handle(_buffer, _socket));
//PacketHandler.Handle(packet, _socket);
_buffer = new byte[1024];
_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, null);
}
private void SentCallback(IAsyncResult result)
{
_socket.EndSend(result);
//_buffer = new byte[1024];
}
public void Item(string text)
{
if (_listbox.InvokeRequired)
{
_listbox.Invoke(new Action<string>(Item), text);
return;
}
_listbox.Items.Add(text);
}
server
private Socket _socket;
private Socket _client;
private byte[] _buffer = new byte[1024];
private ListBox _listbox;
public ServerSocket(ListBox list)
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_listbox = list;
}
public void Bind(int port)
{
_socket.Bind(new IPEndPoint(IPAddress.Any, port));
Item("Server Started");
}
public void Listen(int backlog)
{
_socket.Listen(backlog);
Item("Listening...");
}
public void Accept()
{
_socket.BeginAccept(AcceptedCallback, null);
}
public void SendMessage(string text)
{
_buffer = new byte[1024];
_buffer = Encoding.Default.GetBytes(text);
_client.BeginSend(_buffer, 0, _buffer.Length, SocketFlags.None, SentCallback, null);
}
private void AcceptedCallback(IAsyncResult result)
{
_client = _socket.EndAccept(result);
_buffer = new byte[1024];
_client.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, _client);
//_listbox.Items.Add("Accepted User" + _socket.LocalEndPoint.AddressFamily.ToString() + " ");
//Accept();
}
private void ReceivedCallback(IAsyncResult result)
{
try
{
//_client = result.AsyncState as Socket;
int bufLength = _client.EndReceive(result);
//byte[] packet = new byte[bufferSize];
//Array.Copy(_buffer, packet, packet.Length);
Array.Resize(ref _buffer, bufLength);
Item(PacketHandler.Handle(_buffer, _client));
//Item(PacketHandler.Handle(packet, clientSocket));
//_listbox.Items.Add(PacketHandler.Handle(packet, clientSocket));
_buffer = new byte[1024];
_client.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, _client);
}
catch (SocketException)
{
Item(("User " + _client.LocalEndPoint.ToString() + " has disconnected"));
}
}
private void SentCallback(IAsyncResult result)
{
_client.EndSend(result);
//_buffer = new byte[1024];
}
public void Item(string text)
{
if (_listbox.InvokeRequired)
{
_listbox.Invoke(new Action<string>(Item), text);
return;
}
_listbox.Items.Add(text);
}
cant find a way to fix it, is my code correct?
btw im not a native speaker so bear with my understanding problems :D
_buffer member variable is being used for all send/receive/accept operations. This can cause serious problems, try using separate member buffer variable for each operation.
Related
first - my program works without any problems in a Network with a router. Now I need to establish a connection between two PC's with static IP's, but I get the following message: the requested address is not valid in its context.
Some connect code:
namespace ConsoleUdpTest
{
class Program
{
public static Socket socketReceiver;
public static Socket socketSender;
private const int bufSize = 8 * 1024;
private static State state = new State();
private static EndPoint epFrom = new IPEndPoint(IPAddress.Any, 0);
private static EndPoint epTo = new IPEndPoint(IPAddress.Any, 0);
private static AsyncCallback recv = null;
private static string udpBindingIp = "192.168.2.1";
private static int udpBindingPort = 27000;
private static string udpOutIp = "self";
private static int udpOutPort = 0;
static void Main(string[] args)
{
socketReceiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socketReceiver.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
socketReceiver.Bind(new IPEndPoint(IPAddress.Parse(udpBindingIp), udpBindingPort));
socketSender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socketSender.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
Receive();
while (true)
{
System.Console.ReadKey();
};
}
private static void Send(string text)
{
try
{
socketSender.Connect(epTo);
byte[] data = Encoding.ASCII.GetBytes(text);
socketSender.BeginSend(data, 0, data.Length, SocketFlags.None, (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = socketSender.EndSend(ar);
}, state);
}
catch (Exception e)
{
string s = e.Message;
}
}
private static void Receive()
{
socketReceiver.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
{
try
{
State so = (State)ar.AsyncState;
int bytes = socketReceiver.EndReceiveFrom(ar, ref epFrom);
socketReceiver.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
string message = Encoding.ASCII.GetString(so.buffer, 0, bytes);
if (udpOutIp.Contains("self"))
{
epTo = epFrom;
}
Send(message);
if (message.Contains("(") && message.Contains(")"))
{
Console.WriteLine("contains ( and )");
string command = message.TrimStart();
command = command.Substring(0, command.IndexOf("("));
string parameters = message.Substring(message.IndexOf("(") + 1, message.IndexOf(")") - message.IndexOf("(")-1);
//if (BleCommands.Exists(o => command.Contains(o)))
Console.WriteLine(command);
Console.WriteLine(parameters);
}
Console.WriteLine("in: " + message);
}
catch (Exception e)
{
string s = e.Message;
Console.WriteLine("Exception: " + s);
}
}, state);
}
public class State
{
public byte[] buffer = new byte[bufSize];
}
}
}
Server IP: 192.168.2.1 Subnetmask: 255.255.255.0
Client IP: 192.168.2.2 Subnetmask: 255.255.255.0
Ping the Server works without any problems
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.
I've got a client - server code going on at the moment while working on my thesis.
And I can spawn a connection and send data, but as soon as the client disconnects and tries to reconnect everything goes sideways and I can't seem to figure out why. Too many exceptions are being thrown and I just have no idea where to start catching them all.
What am doing wrong or not doing that isn't allowing nether client nor server to handle disconnections properly ?!
This is my ServerSide Code:
using System;
using System.Net;
using System.Net.Sockets;
namespace Server.Networking
{
public class ServerSocket
{
private Socket _socket;
Byte[] _buffer = new byte[61144];
public ServerSocket()
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void Bind(int port)
{
_socket.Bind(new IPEndPoint(IPAddress.Any, port));
}
public void Listener(int backlog)
{
_socket.Listen(backlog);
}
public void Accept()
{
_socket.BeginAccept(AcceptedCallback, null);
}
private void AcceptedCallback(IAsyncResult result)
{
try
{
Socket clientSocket = _socket.EndAccept(result);
if (clientSocket.Connected)
{
Console.WriteLine("Client has connected!");
_buffer = new byte[61144];
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, clientSocket);
Accept();
}
else
{
Console.WriteLine("Client hasn't connected!");
return;
}
}catch(SocketException ex)
{
Console.WriteLine(ex.Message);
close(clientSocket);
}
}
private void ReceivedCallback(IAsyncResult result)
{
try
{
Socket clientSocket = result.AsyncState as Socket;
SocketError SE;
int bufferSize = clientSocket.EndReceive(result, out SE);
if (bufferSize > 0)
{
if (SE == SocketError.Success)
{
byte[] packet = new byte[bufferSize];
Array.Copy(_buffer, packet, packet.Length);
Console.WriteLine("Handling packet from IP:" + clientSocket.RemoteEndPoint.ToString());
//Handle packet stuff here.
PacketHandler.Handle(packet, clientSocket);
_buffer = new byte[61144];
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, clientSocket);
}
else
{
close(clientSocket);
}
}
else
{
Console.WriteLine("Probably received bad data.");
close(clientSocket);
}
}
catch (SocketException ex)
{
Console.WriteLine(ex.Message);
close(clientSocket);
}
}
public void close(Socket sock)
{
Console.WriteLine("Closing socket for IP:" + sock.RemoteEndPoint.ToString() + " and releasing resources.");
sock.Dispose();
sock.Close();
}
}
}
And this is my ClientSide Code:
using System;
using System.Net;
using System.Linq;
using System.Net.Sockets;
using Client.Networking.Packets;
using System.Net.NetworkInformation;
using Client.Networking.Packets.Request;
namespace Client.Networking
{
public class ClientSocket
{
private Socket _socket;
private byte[] _buffer;
public delegate void RaiseConnect(object source, TextArgs e);
public static event EventHandler Disconnected;
private static void RaiseDisconnect()
{
EventHandler handler = Disconnected;
if(handler !=null)
{
handler(null, EventArgs.Empty);
}
}
public ClientSocket()
{
udpbroadcast.Connect += new RaiseConnect(OnConnectRaise);
}
public string machineIP()
{
return Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork).ToString();
}
private void OnConnectRaise(object sender, TextArgs e)
{
CheckRegisteredRequest computer_name = new CheckRegisteredRequest(Environment.MachineName.ToString() + "," + machineIP());
Connect(e.Message, 6556);
Send(computer_name.Data);
}
public void Connect(string ipAddress, int port)
{
string ip = ipAddress;
int porT = port;
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IAsyncResult result =_socket.BeginConnect(new IPEndPoint(IPAddress.Parse(ip), port), ConnectCallback, null);
bool success = result.AsyncWaitHandle.WaitOne(5000, true);
if(!success)
{
_socket.Close();
Console.WriteLine("Failed to connect to server. Trying again.");
Connect(ip, port);
}
}
private void ConnectCallback(IAsyncResult result)
{
try {
if (_socket.Connected)
{
Console.WriteLine("Connected to the server!");
_buffer = new byte[61144];
_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, null);
}
else
{
Console.WriteLine("Could not connect.");
Close(_socket);
}
}
catch(SocketException ex)
{
Console.WriteLine("ClientSocket ConnectCallback - "+ex.Message);
Close(_socket);
}
}
private void ReceivedCallback(IAsyncResult result)
{
try
{
SocketError SE;
int buflength = _socket.EndReceive(result, out SE);
if (buflength > 0)
{
if(SE == SocketError.Success)
{
byte[] packet = new byte[buflength];
Array.Copy(_buffer, packet, packet.Length);
//Handle the Package
PacketHandler.Handle(packet, _socket);
_buffer = new byte[61144];
_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, null);
}
else
{
Close(_socket);
}
}
else
{
Close(_socket);
}
}
catch (Exception ex)
{
Console.WriteLine("ClientSocket ReceivedCallback - " + ex.Message);
Close(_socket);
}
}
public void Send(byte[] data)
{
byte[] send = new byte[data.Length];
send = data;
if( _socket.Connected)
{
_socket.Send(data);
}
else
{
Console.WriteLine("Not connected yet!");
Close(_socket);
}
}
public bool connectionStatus()
{
return _socket.Connected;
}
public static void Close(Socket sock)
{
Console.WriteLine("Closing the socket and releasing resources.");
sock.Dispose();
sock.Close();
RaiseDisconnect();
}
}
}
Two things I can think of. The documentation on Socket recommends calling Shutdown() first, before Close().
Also, you cannot reuse a socket object. So make sure you're using a new Socket object before trying a new connection, either by _socket = new Socket(), or by using a totally new ServerSocket/ClientSocket.
I managed to get it working with the following code. Within the server code, I initiate the BeginReceive witihn the AcceptedCallback in the following way:
clientSocket.BeginReceive(new byte[] {0}, 0, 0, 0, ReceivedCallback, clientSocket);
Then within the ReceivedCallback, i do BeginReceive differently like this:
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, clientSocket);
While being bothered by the exceptions, it seems that I've skipped checking what I'm receiving in the initial connection.
At the client code, within the ConnectCallback method i do begin receive the other way around:
_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, null);
And within the ReceivedCallback method I did BeginReceive the in reverse:
_socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, ReceivedCallback, null);
After three hours of debugging, it makes sense in a way to me.
The server spawns a new socket object every time it Accepts a connection, in order to avoid threading. So on the Server side at the AcceptedCallback method, the initial _socket.BeginReceive should call the ReceivedCallback method via an empty byte array to only triggers it. Then within the ReceivedCallback you do BeginReceive with an array customized to the length that's been received by the socket. Whereas at Client side, it's reversed.
This is my server side code:
private Socket _socket;
Byte[] _buffer = new byte[61144];
public ServerSocket()
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void Bind(int port)
{
_socket.Bind(new IPEndPoint(IPAddress.Any, port));
}
public void Listener(int backlog)
{
_socket.Listen(backlog);
}
public void Accept()
{
_socket.BeginAccept(AcceptedCallback, null);
}
private void AcceptedCallback(IAsyncResult result)
{
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
Console.WriteLine("We accepted a connection.");
clientSocket = _socket.EndAccept(result);
if (clientSocket.Connected)
{
Console.WriteLine("Client has connected!");
_buffer = new byte[61144];
//clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, clientSocket);
clientSocket.BeginReceive(new byte[] {0}, 0, 0, 0, ReceivedCallback, clientSocket);
Accept();
}
else
{
Console.WriteLine("Client hasn't connected!");
Accept();
}
}
catch (Exception ex)
{
Console.WriteLine("Socket was probably forcefully closed." + ex.Message);
Console.WriteLine("Continue accepting other connections.");
clientSocket.Close();
Accept();
}
}
private void ReceivedCallback(IAsyncResult result)
{
Socket clientSocket = result.AsyncState as Socket;
SocketError ER;
try
{
int bufferSize = clientSocket.EndReceive(result, out ER);
if (ER == SocketError.Success)
{
byte[] packet = new byte[bufferSize];
Array.Copy(_buffer, packet, packet.Length);
Console.WriteLine("Handling packet from IP:" + clientSocket.RemoteEndPoint.ToString());
//Handle packet stuff here.
PacketHandler.Handle(packet, clientSocket);
_buffer = new byte[61144];
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, clientSocket);
//clientSocket.BeginReceive(new byte[] { 0 }, 0, 0, 0, ReceivedCallback, clientSocket);
}
else
{
Console.WriteLine("No bytes received, we're closing the connection.");
clientSocket.Close();
}
}catch(SocketException ex)
{
Console.WriteLine("We caught a socket exception:" + ex.Message);
clientSocket.Close();
}
}
And this is my Client side code:
private Socket _socket;
private CheckRegisteredRequest computer_name;
private bool isClosed;
private byte[] _buffer;
public delegate void RaiseConnect(object source, TextArgs e);
public static event EventHandler Disconnected;
private static void RaiseDisconnect()
{
EventHandler handler = Disconnected;
if (handler != null)
{
handler(null, EventArgs.Empty);
}
}
public ClientSocket()
{
isClosed = true;
udpbroadcast.Connect += new RaiseConnect(OnConnectRaise);
computer_name = new CheckRegisteredRequest(Environment.MachineName.ToString() + "," + machineIP());
}
private void OnConnectRaise(object sender, TextArgs e)
{
if(!isClosed)
{
_socket.Close();
Connect(e.Message, 6556);
}
else
{
Connect(e.Message, 6556);
}
}
public void Connect(string ipAddress, int port)
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.BeginConnect(new IPEndPoint(IPAddress.Parse(ipAddress), port), ConnectCallback, null);
isClosed = false;
/*IAsyncResult result =
bool success = result.AsyncWaitHandle.WaitOne(5000, true);
if (!success)
{
_socket.Shutdown(SocketShutdown.Both);
_socket.Close();
Console.WriteLine("Failed to connect to server. Trying again.");
RaiseDisconnect();
}*/
}
private void ConnectCallback(IAsyncResult result)
{
try
{
if(_socket.Connected)
{
_socket.EndConnect(result);
Console.WriteLine("We initiated a connection to the server.");
Console.WriteLine("Connected to the server!");
Send(computer_name.Data);
_buffer = new byte[61144];
_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, null);
}
else
{
Console.WriteLine("Could not connect.");
if(!isClosed)
{
_socket.Close();
isClosed = true;
RaiseDisconnect();
}
else
{
isClosed = true;
RaiseDisconnect();
}
}
}
catch (SocketException ex)
{
Console.WriteLine("ConnectCallback Exception Caught.");
Console.WriteLine("Shutting down and closing socket for reusal.");
if (!isClosed)
{
_socket.Close();
isClosed = true;
RaiseDisconnect();
}
else
{
isClosed = true;
RaiseDisconnect();
}
}
}
private void ReceivedCallback(IAsyncResult result)
{
try
{
SocketError SE;
int buflength = _socket.EndReceive(result, out SE);
if (SE == SocketError.Success)
{
byte[] packet = new byte[buflength];
Array.Copy(_buffer, packet, packet.Length);
//Handle the Package
PacketHandler.Handle(packet, _socket);
_buffer = new byte[61144];
//_socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, null);
_socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, ReceivedCallback, null);
}
else
{
Console.WriteLine("No bytes received, we're closing the connection.");
if (!isClosed)
{
_socket.Close();
isClosed = true;
RaiseDisconnect();
}
else
{
isClosed = true;
RaiseDisconnect();
}
}
}
catch (Exception ex)
{
Console.WriteLine("ReceivedCallback Exception Caught.");
Console.WriteLine("Shutting down and closing socket for reusal.");
if (!isClosed)
{
_socket.Close();
isClosed = true;
RaiseDisconnect();
}
else
{
isClosed = true;
RaiseDisconnect();
}
}
}
public void Send(byte[] data)
{
byte[] send = new byte[data.Length];
send = data;
if (_socket.Connected)
{
_socket.Send(data);
}
else
{
Console.WriteLine("We're not connected yet!");
if (!isClosed)
{
_socket.Close();
isClosed = true;
RaiseDisconnect();
}
else
{
isClosed = true;
RaiseDisconnect();
}
}
}
public bool connectionStatus()
{
return _socket.Connected;
}
public string machineIP()
{
return Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork).ToString();
}
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)
{
}
}
}
}
Basically I made two C# applications, a client and a server. The client connects to the server (via sockets), then sends a packet containing some text, and the server should reply.
My problem is: the server sends (or the client receives) the response packet only when it closes (ALT+F4). I'd like some help. I'll copypaste below the source code for both the projects.
Client:
public class StateObject
{
public Socket skt = null;
public const int BufferSize = 256;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousClient
{
private const int port = 11000;
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
private static String response = String.Empty;
public static string command;
public static Socket client;
public static void StartClient()
{
try
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(IPAddress.Parse("127.0.0.1"));
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
while (true)
{
command = Console.ReadLine();
if (command == "exit")
{
Console.WriteLine("Terminating...");
client.Shutdown(SocketShutdown.Both);
client.Close();
Environment.Exit(0);
}
else
{
Send(client, command + "<EOF>");
sendDone.WaitOne();
Receive(client);
receiveDone.WaitOne();
Console.WriteLine("Response received : {0}", ProcessResponse(response));
client.Shutdown(SocketShutdown.Both);
client.Close();
}
//Console.CancelKeyPress += (sender, e) =>
//{
// Console.WriteLine("Terminating...");
// client.Shutdown(SocketShutdown.Both);
// client.Close();
// Environment.Exit(0);
//};
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
static public string ProcessResponse(string pkt)
{
string response = null;
response = pkt.Replace("<EOF>","");
return response;
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Receive(Socket client)
{
try
{
StateObject state = new StateObject();
state.skt = client;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.skt;
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Send(Socket client, String data)
{
byte[] byteData = Encoding.ASCII.GetBytes(data);
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args)
{
StartClient();
return 0;
}
Server:
public class Program
{
public class StateObject
{
public Socket skt = null;
public const int buffersize = 1024;
public byte[] buffer = new byte[buffersize];
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousSocketListener
{
public static ManualResetEvent allDone = new ManualResetEvent(false);
public AsynchronousSocketListener() { }
public static Socket handler;
public static void StartListening()
{
byte[] bytes = new Byte[1024];
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
while (true)
{
allDone.Reset();
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
public static void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.skt = handler;
handler.BeginReceive(state.buffer, 0, StateObject.buffersize, 0, new AsyncCallback(ReadCallback), state);
}
public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.skt;
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, ProcessResponse(content));
Send(handler, content);
}
else
{
handler.BeginReceive(state.buffer, 0, StateObject.buffersize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
private static void Send(Socket handler, String data)
{
byte[] byteData = Encoding.ASCII.GetBytes(data);
handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
static public string ProcessResponse(String pkt)
{
string response = null;
response = pkt.Replace("<EOF>", "");
return response;
}
}
public static void Main(string[] args)
{
AsynchronousSocketListener.StartListening();
}
}
In your Client receive callback:
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.skt;
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
You won't ever drop down into the else block unless the socket is explicitly closed (or there is some kind of other error in the connection). Therefore receiveDone never gets set and your main loop is simply stuck waiting for a "response".
If you want to process a "complete message" when it comes in, then check for your <EOF> value after you append the current string to your buffer like this:
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// it's not a "response" unless it's terminated with "<EOF>" right?
response = state.sb.ToString();
if (response.IndexOf("<EOF>") != -1)
{
state.sb.Clear();
receiveDone.Set();
}
else
{
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
}
else
{
if (state.sb.Length > 1)
{
response = state.sb.ToString(); // this is a partial response, not terminated with "<EOF>"
}
receiveDone.Set();
}
Note that response mechanism being used is extremely limited as it would fail for multiple messages coming in at the same time like: Hello<EOF> World!<EOF> It would treat those two messages as one long message. (I realize your example is only sending one "message".)
You're almost certainly going to have to deal with that scenario in any real world application that sends "control" messages in addition to "content" messages. To handle that you'd look for <EOF> using IndexOf() and extract the text up to that point and process that "complete message". Afterwards you'd keep looping as long as <EOF> is still found to process the other pending messages. You'd also have to REMOVE those processed complete messages from the StringBuilder in such a way that any remaining values after the <EOF> are left in place so that when partial messages come in the new data can be appended to the existing data. This is because your data can also be split up when it is sent, resulting in multiple "chunks" of data being received even though it is logically one "complete message" when you sent it. So one send with Complete Message<EOF> could result in one or more receives such as Comp followed by lete Message<EOF>. Your code has to be able to deal with these realities of TCP communication...