I have a little application which sends images from client to all other clients. Actually my buffer size is set to 4096. But I need to have a dynamic size, so that I can send larger images.
Any one of you have an idea what I have to change in my code?
Here is the full code (server and client are in one app):
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace TCPClientServer
{
public partial class Main : Form
{
private Socket clientSocket; //The main client socket
private byte[] clientData = new byte[4096];
private List<Socket> clientSockets = new List<Socket>();
Socket serverSocket; //The main socket on which the server listens to the clients
byte[] serverData = new byte[4096];
public Main()
{
InitializeComponent();
}
#region AsyncCallback
#region ClientCallbacks
private void OnConnect(IAsyncResult result)
{
try
{
clientSocket.EndConnect(result);
clientData = new byte[4096];
//Start listening to the data asynchronously
clientSocket.BeginReceive(clientData, 0, clientData.Length, SocketFlags.None, new AsyncCallback(OnClientReceive), null);
MessageBox.Show("Verbindung wurde hergestellt.");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void OnClientSend(IAsyncResult result)
{
try
{
clientSocket.EndSend(result);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void OnClientReceive(IAsyncResult result)
{
try
{
clientSocket.EndReceive(result);
Image msgReceived = ByteToImage(clientData);
pictureBox1.Image = msgReceived;
clientData = new byte[4096];
clientSocket.BeginReceive(clientData, 0, clientData.Length, SocketFlags.None, new AsyncCallback(OnClientReceive), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
#endregion
#region ServerCallbacks
private void OnAccept(IAsyncResult result)
{
try
{
Socket client = serverSocket.EndAccept(result);
// save new client into clientlist
clientSockets.Add(client);
//Start listening for more clients
serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
//Once the client connects then start receiving the commands from her
client.BeginReceive(serverData, 0, serverData.Length, SocketFlags.None, new AsyncCallback(OnServerReceive), client);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public void OnServerSend(IAsyncResult result)
{
try
{
Socket client = (Socket) result.AsyncState;
client.EndSend(result);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void OnServerReceive(IAsyncResult result)
{
try
{
Socket client = (Socket) result.AsyncState;
client.EndReceive(result);
//Transform the array of bytes received from the user
Image msgReceived = ByteToImage(serverData);
pictureBox1.Image = msgReceived;
//Send the message to all users
foreach (Socket socket in clientSockets)
{
socket.BeginSend(serverData, 0, serverData.Length, SocketFlags.None, new AsyncCallback(OnServerSend), socket);
}
serverData = new byte[4096];
//Start listening to the message send by the user
client.BeginReceive(serverData, 0, serverData.Length, SocketFlags.None, new AsyncCallback(OnServerReceive), client);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
#endregion
#endregion
private void btn_Client_Click(object sender, EventArgs e)
{
try
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
//Server is listening on port 15973
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 15973);
//Connect to the server
clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(OnConnect), null);
btn_Client.Enabled = false;
btn_Server.Enabled = false;
btn_Send.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btn_Server_Click(object sender, EventArgs e)
{
try
{
//We are using TCP sockets
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//Assign the any IP of the machine and listen on port number 15973
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 15973);
//Bind and listen on the given address
serverSocket.Bind(ipEndPoint);
serverSocket.Listen(4);
//Accept the incoming clients
serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
btn_Client.Enabled = false;
btn_Server.Enabled = false;
btn_Send.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btn_Send_Click(object sender, EventArgs e)
{
try
{
Image img = new Bitmap(#"C:\Users\Kai\Desktop\microsoft-windows-10-icon.png");
clientData = ImageToByte(img);
clientSocket.BeginSend(clientData, 0, clientData.Length, SocketFlags.None, new AsyncCallback(OnClientSend), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[]) converter.ConvertTo(img, typeof(byte[]));
}
private Image ByteToImage(byte[] bytes)
{
ImageConverter converter = new ImageConverter();
return (Image) converter.ConvertFrom(bytes);
}
}
}
Thx in advance :)
This might help.
private int getImageBufferLength(Image image)
{
MemoryStream mStream = new MemoryStream();
image.Save(mStream, image.RawFormat);
int length = mStream.ToArray().Length;
mStream.Dispose();
return length;
}
Related
I have setup a client server chat system. That will have to work on different computers. At the moment I am testing and so both client and server are on my localhost.
At the moment I focus on the client reception (with a 8192 byte buffer) for it is that the problem resides.
And the problem is that when issue a command with very short text (here 123):
Server SHORT_COMMAND 123% ---> Client SHORT_COMMAND 123%
but when I issue a very long command (24k) each single batch does not arrive in the correct order
E.g. imagine a very long text 111.....11112222....2222333....33334444....4444
LONG_COMMAND 111....3333% is therefore automatically cut into
LONG_COMMAND 111....1111
2222....2222
3333...3333
4444...4444%
and sent in that order
what is wrong is the reception and what might happen is to receive
LONG_COMMAND 111....1111
2222....2222
4444...4444%
3333...3333
or it might get mixed with other short commands like
LONG_COMMAND 111....1111
2222....2222
SHORT_COMMAND 123%
3333...3333
4444...4444%
Take into account that some messages can be sent very quickly one after the other.
Thank you for ANY help
Patrick
Client code
I start the client with the following routine. In my case I pass -1
public static bool StartClient(string strIpAddress, string strPort)
{
// Connect to a remote device.
try
{
if (strIpAddress.Trim() == "-1")
strIpAddress = GetLocalIPAddress();
IPAddress ipAddress = IPAddress.Parse(strIpAddress);
int port = int.Parse(strPort);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
socketClient.BeginConnect(remoteEP, new AsyncCallback(ConnectClientCallback), socketClient);
OnNotification_Client?.Invoke(eSocketOperation.CONNECT, "Client connected to " + strIpAddress + " port=" + strPort, eSocketOperationResultType.SUCCESS);
ReceiveClient();
return true;
}
catch (Exception e)
{
MessageBox.Show("StartClient exc:" + e.ToString());
return false;
}
}
then
private static void ConnectClientCallback(IAsyncResult ar)
{
try
{
socketClient = (Socket)ar.AsyncState;
socketClient.EndConnect(ar);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
The following part is the reception with:
public static void ReceiveClient()
{
try
{
StateObject state = new StateObject();
state.workSocket = socketClient;
socketClient.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback_Client), state);
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "ReceiveClient", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
finally
private static void ReceiveCallback_Client(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
if (!client.Connected)
return;
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
string message = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
if (message == null)
OnNotification_Client?.Invoke(eSocketOperationResultType.ERROR, "Received null message from server", eSocketOperationResultType.ERROR);
else
{
strReceivedMessageClient += message;
if (strReceivedMessageClient.EndsWith(StateObject.CONFIRMATION.ToString()))
{
...
Server Code
The server is started with
public static void StartServer(string _strPort)
{
Serializers.Logger.WriteLog("StartServer");
StrPort = _strPort;
// Data buffer for incoming data.
byte[] bytes = new Byte[BufferSize];
IPAddress ipAddress = IPAddress.Parse(GetLocalIPAddress());
int port = int.Parse(_strPort);
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
if (listener == null)
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
OnNotification_Server?.Invoke("Waiting for a connection on " + ipAddress.ToString());
listener.BeginAccept(new AsyncCallback(AcceptCallbackServer), listener);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
Accept
public static void AcceptCallbackServer(IAsyncResult ar)
{
if (listener == null)
return;
listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback_Server), state);
}
and last but not least the send:
public static void SendFromServerToClient(object command, String data, eSocketOperationResultType sort)
{
string strMessage; ;
strMessage = command.ToString() + '|' + data + '|' + sort + "%";
byte[] byteData = Encoding.ASCII.GetBytes(strMessage);
try
{
if (socketServer != null)
socketServer.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), socketServer);
}
catch (Exception exc)
{
}
}
private static void SendCallback(IAsyncResult ar)
{
try
{
Socket handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
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 have searched many examples and tutorials and what not but I cant for the life of me figure out what im doing wrong here... If I send several messages to this server I made only the first is printed in the Console.Writeline command and the rest is never printed... I must be doing something fundametally wrong but I really cant find it ... :S
This is the server code:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms.VisualStyles;
namespace HL7_Manager
{
public class MonitorServer
{
private int _port;
private Socket _serverSocket;
private List<ClientObject> _clients;
public bool IsConnected { get; set; }
public MonitorServer(int port)
{
_port = port;
_clients = new List<ClientObject>();
}
public void StartListening()
{
_serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Thread listenThread = new Thread(new ThreadStart(ListenerThread));
listenThread.IsBackground = true;
listenThread.Start();
}
public void StopListening()
{
IsConnected = true;
_serverSocket.Close();
while (_clients.Count > 0)
{
_clients[0].KeepProcessing = false;
_clients[0].ClientSocket.Close();
_clients.RemoveAt(0);
}
}
private void ListenerThread()
{
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, _port));
_serverSocket.Listen(100);
Console.WriteLine("Listening on port 8000");
while (true)
{
Socket clientSocket = _serverSocket.Accept();
ClientObject client = new ClientObject();
client.KeepProcessing = true;
client.ClientSocket = clientSocket;
_clients.Add(client);
ParameterizedThreadStart ptStart = new ParameterizedThreadStart(ProcessClientThread);
Thread processThread = new Thread(ptStart);
processThread.IsBackground = true;
processThread.Start(client);
clientSocket = null;
client = null;
}
}
private void ProcessClientThread(object clientObj)
{
Console.WriteLine("Client connected");
ClientObject client = (ClientObject) clientObj;
Socket clientSocket = client.ClientSocket;
byte[] buffer = new byte[clientSocket.ReceiveBufferSize];
int receiveCount = 0;
while (client.KeepProcessing)
{
try
{
receiveCount = clientSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
Console.WriteLine(Encoding.ASCII.GetString(buffer));
}
catch (Exception ex)
{
if (!client.KeepProcessing)
return;
Console.WriteLine(ex.Message);
}
}
clientSocket.Close();
_clients.Remove(client);
}
}
}
Here is the method you should definitely change and how to change it.
private void ProcessClientThread(object clientObj)
{
Console.WriteLine("Client connected");
ClientObject client = (ClientObject)clientObj;
Socket clientSocket = client.ClientSocket;
byte[] buffer = new byte[clientSocket.ReceiveBufferSize];
int receiveCount = 0;
while (client.KeepProcessing)
{
try
{
receiveCount = clientSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
if (receiveCount == 0)
break; //the client has closed the stream
var ret = Encoding.ASCII.GetString(buffer, 0, receiveCount);
Console.WriteLine(ret);
}
catch (Exception ex)
{
if (!client.KeepProcessing)
return;
Console.WriteLine(ex.Message);
}
}
clientSocket.Close();
_clients.Remove(client);
}
Check how many bytes you really received.
TCP is a streaming protocol this means that if you client is doing several sends of small messages right one after the other, you will receive them in one go at the receiver.
If there happens to be a null character in your receive buffer you might think you did not receive all those string, but actually you did.
Check this by inspecting how many bytes you received and by checking the buffer content.
If you made this mistake there might be some deeper problem in your code. The fact that TCP is streaming makes it a bit more complex
I'm trying to create this simple client-server. I have an android client and c#server. just a simple program that sends a hello message to the server but the message isn't sent.
my java code:
Thread t= new Thread()
{
#Override
public void run() {
// TODO Auto-generated method stub
try {
Socket myClient= new Socket("192.167.01.123",7000);
DataOutputStream dos= new DataOutputStream(myClient.getOutputStream());
dos.writeBytes("Hello");
dos.flush();
dos.close();
myClient.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.println("unknown host");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IOExxception");
}
}
};
t.start();
Toast.makeText(this," Message sent" , Toast.LENGTH_SHORT).show();
}
and c# code:
static void Main(string[] args)
{
TcpListener serverSocket = new TcpListener(7000);
TcpClient clientSocket = new TcpClient();
serverSocket.Start();
Console.WriteLine("Server started.");
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine("Accept conns from client.");
while (true)
{
try
{
NetworkStream networkStream = clientSocket.GetStream();
Byte[] bytes = new Byte[10025];
networkStream.Read(bytes, 0, (int)clientSocket.ReceiveBufferSize);
string dataClient = System.Text.Encoding.ASCII.GetString(bytes);
Console.WriteLine("data from client: " + dataClient);
networkStream.Flush();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine("EXIT");
Console.ReadKey();
}
}
}
Should be inside the while loop
clientSocket = serverSocket.AcceptTcpClient();
Try this:
c# Server Class to Receive Message :
class Server
{
int BUFSIZE = 100;
int servPort = 1551;
public static String ClientOrder = "";
public static int bytesRcvd;
public void RunServer()
{
TcpListener listener = null;
TcpClient client = null;
NetworkStream netStream = null;
listener = new TcpListener(IPAddress.Any, servPort);
listener.Start();
byte[] rcvBuffer = new byte[BUFSIZE];
while (true)
{
client = listener.AcceptTcpClient(); // Get clien
netStream = client.GetStream();
{
rcvBuffer = new byte[BUFSIZE];
bytesRcvd = netStream.Read(rcvBuffer, 0, rcvBuffer.Length);
ClientOrder = (Encoding.ASCII.GetString(rcvBuffer)).Substring(0, bytesRcvd);
netStream.Close();
client.Close();
}
}
}
}
Android Client Class to Send Message:
public class Send_Message_To_Server implements Runnable {
private String mMsg,Server;
private Socket client;
private PrintWriter printwriter;
public Send_Order_To_Server(String msg,String IP) {
mMsg = msg;
Server=IP;
}
public void run() {
try {
client = new Socket(Server, 1551); //connect to server
printwriter = new PrintWriter(client.getOutputStream(),true);
printwriter.write(mMsg); //write the message to output stream
printwriter.flush();
printwriter.close();
client.close(); //closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Ok so I have a socket connection within my code that is causing a problem. It connects and then disconnects rather quickly. It disconnects after the "connections++;" line. Any ideas?
AS REQUESTED ALL CODE FOR THIS FORM BELOW
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace Middleware
{
public partial class Middleware : Form
{
//variables
private Socket server;
private Socket remoteclient;
private Socket clientreturn;
private Socket serversync;
private byte[] data = new byte[1024];
private byte[] datars = new byte[1024];
private int connections = 0;
public Middleware()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void Middleware_Load(object sender, EventArgs e)
{
}
void OnConnectedRemote(IAsyncResult result)
{
try
{
remoteclient.EndConnect(result);
}
catch
{
remoteclient.Close();
}
}
void OnConnected(IAsyncResult result)
{
Socket client = server.EndAccept(result);
connections++;
server.BeginAccept(new AsyncCallback(OnConnected), null);
try
{
txtStatus.Text = "" + connections;
byte[] message = Encoding.ASCII.GetBytes("Welcome to my server");
client.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnDataSent), client);
}
catch
{
client.Close();
}
}
void OnConnectedSync(IAsyncResult result)
{
Socket sync = serversync.EndAccept(result);
connections++;
//serversync.BeginAccept(new AsyncCallback(OnConnectedSync), null);
try
{
txtStatus.Text = "" + connections;
byte[] message = Encoding.ASCII.GetBytes("Connected to Middleware");
sync.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(OnDataSentSync), sync);
}
catch
{
sync.Close();
}
}
void OnDataSent(IAsyncResult result)
{
Socket client = (Socket)result.AsyncState;
try
{
//end send and begin receive from client
int sent = client.EndSend(result);
client.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), client);
}
catch (SocketException)
{
//close client
client.Close();
}
}
void OnDataSentSync(IAsyncResult result)
{
Socket sync = (Socket)result.AsyncState;
try
{
//end send and begin receive from client
int sent = sync.EndSend(result);
sync.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnDataReceivedSync), sync);
}
catch (SocketException)
{
//close client
sync.Close();
}
}
void OnDataReceivedSync(IAsyncResult result)
{
Socket sync = (Socket)result.AsyncState;
//clientreturn = (Socket)result.AsyncState;
}
void OnDataSentWaiting(IAsyncResult result)
{
Socket sync = (Socket)result.AsyncState;
try
{
//end send and begin receive from client
int sent = sync.EndSend(result);
//sync.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), sync);
}
catch (SocketException)
{
//close client
sync.Close();
}
}
void OnDataReceived(IAsyncResult result)
{
Socket client = (Socket)result.AsyncState;
clientreturn = (Socket)result.AsyncState;
try
{
//if nothing is received then close connection
//otherwise get message and add to list box
//create newsocket, bind and listen to create server connection
//begin accept
int receive;
receive = client.EndReceive(result);
//string port = (((IPEndPoint)client.RemoteEndPoint).Port.ToString ());
if (receive == 0)
{
client.Close();
return;
}
else
{
string message = Encoding.ASCII.GetString(data, 0, receive);
lstAll.Items.Add(message);
txtSent.Text = message;
byte[] echomessage = Encoding.ASCII.GetBytes(message);
bool check = remoteclient.Poll(1000, SelectMode.SelectRead);
bool avail = (remoteclient.Available == 0);
if (check & avail)
{
btnConnect.Enabled = true;
btnDisconnect.Enabled = false;
MessageWait(message);
//serversync.BeginSend(echomessage, 0, echomessage.Length, SocketFlags.None, new AsyncCallback(OnDataSentWaiting), serversync);
//client.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), client);
}
else
{
client.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), client);
remoteclient.BeginSend(echomessage, 0, echomessage.Length, SocketFlags.None, new AsyncCallback(OnRemoteDataSent), remoteclient);
}
}
}
catch (SocketException)
{
//close client
client.Close();
}
}
private void MessageWait(string message)
{
byte[] echomessage = Encoding.ASCII.GetBytes(message);
serversync.BeginSend(echomessage, 0, echomessage.Length, SocketFlags.None, new AsyncCallback(OnDataSentSync), null);
}
void OnRemoteDataSent(IAsyncResult result)
{
try
{
int sent = remoteclient.EndSend(result);
remoteclient.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnRemoteDataReceived), null);
}
catch (SocketException)
{
//close server connection
remoteclient.Close();
}
}
void OnRemoteDataReceived(IAsyncResult result)
{
try
{
int receive = remoteclient.EndReceive(result);
string message = Encoding.ASCII.GetString(data, 0, receive);
txtReceived.Text = message + " from Middle";
datars = Encoding.ASCII.GetBytes(txtReceived.Text);
clientreturn.BeginSend(datars, 0, datars.Length, SocketFlags.None, new AsyncCallback(OnDataSentBack), clientreturn);
}
catch (SocketException)
{
//close server
clientreturn.Close();
}
}
void OnDataSentBack(IAsyncResult result)
{
Socket client = (Socket)result.AsyncState;
try
{
int sent = client.EndSend(result);
}
catch (SocketException)
{
//closeserver
client.Close();
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
btnConnect.Enabled = false;
remoteclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), 2000);
remoteclient.BeginConnect(remoteEndPoint, new AsyncCallback(OnConnectedRemote), null);
}
catch
{
remoteclient.Close();
}
}
private void btnStart_Click(object sender, EventArgs e)
{
int port;
port = int.Parse(txtPort.Text);
btnStart.Enabled = false;
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP = new IPEndPoint(0, port);
server.Bind(localEP);
server.Listen(4);
server.BeginAccept(new AsyncCallback(OnConnected), null);
txtStatus.Text = "Waiting for client...";
}
private void btnSyncC_Click(object sender, EventArgs e)
{
int port;
port = int.Parse(txtPort.Text);
btnStart.Enabled = false;
serversync = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP2 = new IPEndPoint(0, port);
serversync.Bind(localEP2);
serversync.Listen(1);
serversync.BeginAccept(new AsyncCallback(OnConnectedSync), null);
txtStatus.Text = "Waiting for sync client...";
}
}
}
I bet that there is something wrong with the txtStatus object, and you are actually getting a NullReferenceException which is caught, ignored, and causes the socket to immediately close.