I am a Newbie to TCP/IP programming in c#, so I am DESPERATE for a solution to my current problem!
I have designed a C# Windows application running under Windows 7 with a Sqlserver 2005 database. My application is trying to send an HL7 record over a TCP/IP connection, to a Unix Lab. system machine.
I can get a connection ok, and send the HL7. BUT I cannot get ANY reply from the Lab. server! the connection times-out with error code 10060, as well as a _COMPlusExceptionCode value of -532462766.
Here is a sample of my c# 'Connect' methods:
buildSendHL7_TCPIP hl7Agent = new buildSendHL7_TCPIP();
// class 'buildSendHL7_TCPIP(); ' contains methods to build the HL7 segments, as well as methods to send and receive messages over the TCP connection
string strServerIPAddress = string.Empty;
Int32 intSocketNo = new Int32();
bool sentOK = false;
/***********************************/
/*Try send the HL7 to LIS-HORIZON...*/
/***********************************/
strServerIPAddress = "10.1.6.248";
intSocketNo = 5910;
sentOK = hl7Agent.SendHL7(strServerIPAddress, intSocketNo, strHL7_Record);
if (!sentOK)
{
_strUIMessage = "*Error* HL7 Message NOT sent to LIS!";
opsMessageBox mb = new opsMessageBox(this);
mb.ShowDialog();
mb.Close();
goto EndLabel;
}
Here are the methods I've created to build a TCP connection and send the HL7 to the LIS Server:
public bool SendHL7(string strIPAddress, Int32 intSocket, string hl7message)
{
/* send the complete HL7 message to the server...*/
int port = (int)intSocket;
IPAddress localAddr = IPAddress.Parse(strIPAddress);
try
{
// Add the leading & trailing character field-separator and CR LineFeed' t.
string llphl7message = null;
llphl7message = "|";
llphl7message += hl7message;
llphl7message += Convert.ToChar(28).ToString();
llphl7message += Convert.ToChar(13).ToString();
// Get the size of the message that we have to send.
Byte[] bytesToSend = Encoding.ASCII.GetBytes(llphl7message);
Byte[] bytesReceived = new Byte[256];
// Create a socket connection with the specified server and port.
Socket s = ConnectSocket(localAddr, port);
// If the socket could not get a connection, then return false.
if (s == null)
return false;
// Send message to the server.
s.Send(bytesToSend, bytesToSend.Length, 0);
// Receive the response back
int bytes = 0;
s.ReceiveTimeout = 3000; /* 3 seconds wait before timeout */
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0); /* IMEOUT occurs!!! */
string page = Encoding.ASCII.GetString(bytesReceived, 0, bytes);
s.Close();
// Check to see if it was successful
if (page.Contains("MSA|AA"))
{
return true;
}
else
{
return false;
}
}
catch (SocketException e)
{
MessageBox.Show("SocketExecptionError:" + e);
return false;
}
}
private static Socket ConnectSocket(IPAddress server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket = new Socket(ipe.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
tempSocket.Connect(ipe);
if (tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}
I've been told that socket 5910 cannot receive ANY communications in Windows 7 due to Virus issues. Is this true? If so, I tried to connect to ANOTHER server on our network (PACS/RIS) socket # 5556. I get the SAME timeout error message.
The behaviour looks like the server doesn't understand your request / doesn't recognize it as a complete message according to the expected protocol.
As I understand from your code you are sending a HL7 message. I don't know this protocol, according to google it could be Health Level 7. The example I found is starting with some text, then a | as a delimiter. Your message is starting with |. Maybe there is the problem...
So you should have a look if the message you send is matching the protocol definition.
Related
I have a c# client. The client's task is to log in to the server.
The issue is:
When I want to log in, I use a TCP socket which is created when the "wpf" window is initialized. After sending data using the socket once, everything is ok but when I want to send data again, using the same socket, this exception pops up:
System.ObjectDisposedException: 'Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.'
After some testing, I found out that the problem is caused by the Socket.Receive function. I checked the socket before the function was called, and the socket was connected (Socket.Connected == true), after returning from the function, the socket wasn't connected (Socket.Connected == false)
private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
// Get host related information.
IPHostEntry hostEntry = Dns.GetHostEntry(server);
// Loop through the AddressList to obtain the supported AddressFamily.
foreach (IPAddress address in hostEntry.AddressList)
{
//attempting to connect.
IPEndPoint ipe = new IPEndPoint(address, port);
//making a temp socket to check the connection (if something went wronge/ the server isnt active)
Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
// attempting connection.
tempSocket.Connect(ipe);
}
catch (Exception)
{
Console.WriteLine("Request timed out");
}
//if we connected to the server, we are ok to continue.
if (tempSocket.Connected)
{
s = tempSocket;
break;
}
//else the connection isnt successful (the server might not respond) we need to try again.
else
{
continue;
}
}
Globals.SOCKET = s;
return s;
}
//This func will send a request to the server and returns the server's response.
public static string SocketSendReceive(string server, int port, string request, Socket socket = null)
{
Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
string response = "";
// Create a socket connection with the specified server and port.
if (socket == null)
socket = ConnectSocket(server, port);
using (socket)
{
// If the connection faild and couldnt maintain a socket.
if (socket.Connected == false)
return ("Connection failed");
// Send request to the server.
socket.Send(bytesSent, bytesSent.Length, 0);
// Receiving the packet from the server.
int bytes = socket.Receive(bytesReceived, bytesReceived.Length,0);//***The problem occures Here***
response = response + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
return response;
}
the second function is the one that sends and receives data from the server.
the first one is just connecting and creating a socket
Thanks for your help!
-Anthon
Change
socket.Receive(bytesReceived, bytesReceived.Length,0)
To
socket.Receive(bytesReceived, 0, bytesReceived.Length)
I'm implementing an application in .Net. I have to create a connection by SSH which is works, but the HL7 data receiving fails. The destination is a raspberry pi. So when I'm debugging the ssh client is connected, the port is forwarded, the tcp client also connected, but there is no answer for my queries. Plese suggest me some examples!
In this project I have already implemented it on Android - it works fine.
So in .Net I tried the NHapiTools library and I also tried the direct TcpClient way too. localPort = remotePort. I used localIP = "localhost"
static void Main(string[] args)
{
try
{
PrivateKeyFile file = new PrivateKeyFile(#"./key/private.key");
using (var client = new SshClient(remoteIP, sshPort, username, file))
{
client.Connect();
var ci = client.ConnectionInfo;
var port = new ForwardedPortLocal(localIP, localPort, client.ConnectionInfo.Host, remotePort);
client.AddForwardedPort(port);
port.Start();
var req = "MSH|^~\\&|TestAppName||AVR||20181107201939.357+0000||QRY^R02^QRY_R02|923456|P|2.5";
////TCP
var tcpClient = new TcpClient();
tcpClient.Connect(localIP, (int)localPort);
Byte[] data = System.Text.Encoding.ASCII.GetBytes(req);
using (var stream = tcpClient.GetStream())
{
stream.Write(data, 0, data.Length);
using (var buffer = new MemoryStream())
{
byte[] chunk = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(chunk, 0, chunk.Length)) > 0)
{
buffer.Write(chunk, 0, bytesRead);
}
data = buffer.ToArray();
}
}
//I used this also with same result -> no respond
//SimpleMLLP
/*
var connection = new SimpleMLLPClient(localIP, localPort,
Encoding.UTF8);
var response = connection.SendHL7Message(req);
*/
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
}
}
So I experinced that the buffer size is 0 in TCP (due to time out). In the SimpleMLLP test SendHK7Message method never returns
You are not implementing MLLP (also called LLP) protocol while sending message.
Description HEX ASCII Symbol
Message starting character 0B 11 <VT>
Message ending characters 1C,0D 28,13 <FS>,<CR>
This way, when you send a message to Listener (TCP/MLLP server), it looks for Start Block in your incoming data. It never finds it. It just discards your entire message considering garbage. Hence, you get nothing back from Listener.
With MLLP implemented, your message (the stuff you are writing on socket) should look something like below:
<VT>MSH|^~\\&|TestAppName||AVR||20181107201939.357+0000||QRY^R02^QRY_R02|923456|P|2.5<FS><CR>
Note the <VT>, <CR> and <FS> are place holders in above message.
You may refer to this article for detailed information (Read step 4 and onward):
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace SimpleMllpHl7ClientAdvanced
{
public class Program
{
private static char END_OF_BLOCK = '\u001c';
private static char START_OF_BLOCK = '\u000b';
private static char CARRIAGE_RETURN = (char)13;
static void Main(string[] args)
{
TcpClient ourTcpClient = null;
NetworkStream networkStream = null;
var testHl7MessageToTransmit = new StringBuilder();
//a HL7 test message that is enveloped with MLLP as described in my article
testHl7MessageToTransmit.Append(START_OF_BLOCK)
.Append("MSH|^~\\&|AcmeHIS|StJohn|CATH|StJohn|20061019172719||ORM^O01|MSGID12349876|P|2.3")
.Append(CARRIAGE_RETURN)
.Append("PID|||20301||Durden^Tyler^^^Mr.||19700312|M|||88 Punchward Dr.^^Los Angeles^CA^11221^USA|||||||")
.Append(CARRIAGE_RETURN)
.Append("PV1||O|OP^^||||4652^Paulson^Robert|||OP|||||||||9|||||||||||||||||||||||||20061019172717|20061019172718")
.Append(CARRIAGE_RETURN)
.Append("ORC|NW|20061019172719")
.Append(CARRIAGE_RETURN)
.Append("OBR|1|20061019172719||76770^Ultrasound: retroperitoneal^C4|||12349876")
.Append(CARRIAGE_RETURN)
.Append(END_OF_BLOCK)
.Append(CARRIAGE_RETURN);
try
{
//initiate a TCP client connection to local loopback address at port 1080
ourTcpClient = new TcpClient();
ourTcpClient.Connect(new IPEndPoint(IPAddress.Loopback, 1080));
Console.WriteLine("Connected to server....");
//get the IO stream on this connection to write to
networkStream = ourTcpClient.GetStream();
//use UTF-8 and either 8-bit encoding due to MLLP-related recommendations
var sendMessageByteBuffer = Encoding.UTF8.GetBytes(testHl7MessageToTransmit.ToString());
if (networkStream.CanWrite)
{
//send a message through this connection using the IO stream
networkStream.Write(sendMessageByteBuffer, 0, sendMessageByteBuffer.Length);
Console.WriteLine("Data was sent data to server successfully....");
var receiveMessageByteBuffer = Encoding.UTF8.GetBytes(testHl7MessageToTransmit.ToString());
var bytesReceivedFromServer = networkStream.Read(receiveMessageByteBuffer, 0, receiveMessageByteBuffer.Length);
// Our server for this example has been designed to echo back the message
// keep reading from this stream until the message is echoed back
while (bytesReceivedFromServer > 0)
{
if (networkStream.CanRead)
{
bytesReceivedFromServer = networkStream.Read(receiveMessageByteBuffer, 0, receiveMessageByteBuffer.Length);
if (bytesReceivedFromServer == 0)
{
break;
}
}
}
var receivedMessage = Encoding.UTF8.GetString(receiveMessageByteBuffer);
Console.WriteLine("Received message from server: {0}", receivedMessage);
}
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
catch (Exception ex)
{
//display any exceptions that occur to console
Console.WriteLine(ex.Message);
}
finally
{
//close the IO strem and the TCP connection
networkStream?.Close();
ourTcpClient?.Close();
}
}
}
}
You should modify your following line of code as below:
var req = START_OF_BLOCK + "MSH|^~\\&|TestAppName||AVR||20181107201939.357+0000||QRY^R02^QRY_R02|923456|P|2.5" + END_OF_BLOCK + CARRIAGE_RETURN;
For more open source code, you may refer to this github project.
After days of struggling I have solved the problem. The main error was with the port forwarding.
I would reccomend to use SSH.Net by Renci (There was algorithm error with Tamir ssh).
After ssh connection created I used this to port forward:
var port = new ForwardedPortLocal(localIP, localPort, "localhost", remotePort);
Check your localIP with ipconfig /all in cmd. Or use 127.0.0.1 as a loopback IP.
SimpleMLLPClient did not worked for me so I used the direct tcp client query way. Like this:
TcpClient ourTcpClient = new TcpClient();
ourTcpClient.Connect(localIP, (int)localPort);
NetworkStream networkStream = ourTcpClient.GetStream();
var sendMessageByteBuffer = Encoding.UTF8.GetBytes(testHl7MessageToTransmit.ToString());
if (networkStream.CanWrite)
{
networkStream.Write(sendMessageByteBuffer, 0, sendMessageByteBuffer.Length);
Console.WriteLine("Data was sent to server successfully....");
byte[] receiveMessageByteBuffer = new byte[ourTcpClient.ReceiveBufferSize];
var bytesReceivedFromServer = networkStream.Read(receiveMessageByteBuffer, 0, receiveMessageByteBuffer.Length);
if (bytesReceivedFromServer > 0 && networkStream.CanRead)
{
receivedMessage.Append(Encoding.UTF8.GetString(receiveMessageByteBuffer));
}
var message = receivedMessage.Replace("\0", string.Empty);
Console.WriteLine("Received message from server: {0}", message);
}
So it gave me instant answer with 0 bytes (not due timeout). And here comes Amit Joshi help. I used a query what he suggested with START_OF_BLOCK, CARRIAGE_RETURN and END_OF_BLOCK and finally started to work. Thank you Amit Joshi!
Additional info:
In Android (java/Kotlin) jsch session setPortForwardingL works fine with three params:
val session = jsch.getSession("user", sshIP, sshPort)
session.setPassword("")
jsch.addIdentity(privatekey.getAbsolutePath())
// Avoid asking for key confirmation
val prop = Properties()
prop.setProperty("StrictHostKeyChecking", "no")
session.setConfig(prop)
session.connect(5000)
session.setPortForwardingL(localForwardPort, "localhost", remotePort)
val useTls = false
val context = DefaultHapiContext()
connection = context.newClient("localhost", localForwardPort, useTls)
I am trying to discover a Belkin Wemo switch using C#. I am sending SSDP over the network to get back a response from the switch.
The following snippet creates a socket, sends SSDP and waits for 2 seconds to receive an answer. If nothing is read, it starts all again.
bool repeat = true;
while (repeat)
{
UdpClient udpClient = null;
try
{
// Creates the socket.
udpClient = new UdpClient(10140);
udpClient.Client.ReceiveTimeout = 2000;
IPAddress broadcastIpAddress = IPAddress.Parse("239.255.255.250");
IPEndPoint broadcastIpEndPoint = new IPEndPoint(broadcastIpAddress, 1900);
udpClient.JoinMulticastGroup(broadcastIpAddress);
// Sends SSDP.
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("M-SEARCH * HTTP/1.1\r\n");
stringBuilder.Append("ST: urn:Belkin:service:basicevent:1\r\n");
stringBuilder.Append("MX: 1\r\n");
stringBuilder.Append("MAN: \"ssdp:discover\"\r\n");
stringBuilder.Append("HOST: 239.255.255.250:1900\r\n");
stringBuilder.Append("\r\n");
byte[] bytesToSend = Encoding.UTF8.GetBytes(stringBuilder.ToString());
udpClient.Send(bytesToSend, bytesToSend.Length, broadcastIpEndPoint);
// Receives response.
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] receivedBytes = udpClient.Receive(ref remoteIpEndPoint);
string receivedString = Encoding.UTF8.GetString(receivedBytes);
Console.WriteLine(receivedString);
repeat = false;
}
catch (SocketException) { }
finally
{
udpClient.Close();
}
}
Most of the time, a response is returned by the switch. However, sometimes it loops indefinitely without getting any feedback.
I am using Wireshark. In the second case, it happens that no SSDP is sent. I have no explanation about that. My system is Windows 7, maybe it helps...
I am trying to develop a software (in C#) that collect some data on a list (List<string> socketList). The value of this list must be sent to a processing software.
In this program, the server (the C# software) waits for a message from a client (the Processing software) and send an element of the list. This list is continuously filled by a method in another thread (I verify, the list is everytime full). I used this method because I need that each value has to arrive at a client, no matter the speed.
Here there is the method in C# (it is launched as a thread):
public static void InizializeSocket()
{
//setting the comunication port
int port = 5000;
//infinite loop in order to accept sequential clients
while (true)
{
//I open the listener for any IP
TcpListener listener = new TcpListener(IPAddress.Any, port);
Console.WriteLine("Listening...");
//Waiting for a client
listener.Start();
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("CONNECTED");
//this string will contain the client message
String dataReceived = "";
//loop until "q". If the client send a message with a q, the server disconnect the client
while (dataReceived != "q")
{
//Read the client message
NetworkStream nwStream = client.GetStream();
try
{
//Define the client message buffer dimension and read the message
byte[] buffer = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
//Encoding the byte in a string
dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
//check if the List<string> socketList is not empty. If it has any elements, I send the first one and after that I remove the element from the list
if (socketList.Count > 0)
{
//Encoding the string in a byte
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(socketList[0]);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
socketList.RemoveAt(0);
}
nwStream.Flush();
}
catch (Exception e) { dataReceived = "q"; }
}
//Exit from the client loop
Console.WriteLine("DISCONNECTED");
client.Close();
listener.Stop();
}
}
And here the simple Processing software
import processing.net.*;
Client myClient;
String dataIn;
int port = 5000;
String ip = "127.0.0.1";
void setup () {
size(1100, 1025);
//Initialize the client
myClient = new Client(this, "127.0.0.1", 5000);
}
void draw () {
//If the client is available it send a generic message to the server and try to receive a response
if (myClient.available() > 0) {
myClient.write("c");
dataIn = myClient.readString();
}
//print the output in debug
println(dataIn);
}
Here the Processing software is able to connect, anyway, I received every time null.
In addition, if I try to write (in the try on the C# software) only:
if (socketList.Count > 0)
{
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(socketList[0]);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
socketList.RemoveAt(0);
}
I receive strange values that are not the same values in the list stored in C# software.
So, why in the first case processing read only null value? And why in the second case it read "random" value?
EDIT:
Analyze better the programs flow, I verify where is the problem. I rewrite the processing socket as a java socket. It is launched in another thread and fills an ArrayList.
Considering a simplification of the code (in order to understand better), if I write:
Socket s = new Socket("localhost", 5000);
BufferedReader input =new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out= new PrintWriter(s.getOutputStream(), true);
out.println("hello");
String temp = input.readLine();
the software blocks on String temp = input.readLine();. Anyway, the communication is established and I am sure that C# send the string.
What I am trying to do is just check the messages that the client is sending.
what I want is to maintain the socket that was created open and just check the get message that the client send from time to time.
I am using System.Net.Sockets only, I want to make an implementation based on barkeley sockets.
Here is my code,
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
namespace Socket_v1._0
{
class Program
{
private const int BUFSIZE = 200;// Size of receive buffer
private const int BACKLOG = 5; // O valor backlog define o número de pedidos de conexão que podem ser mantidos em espera sem serem aceites pela "system-call" accept.
static void Main(string[] args)
{
int servPort = 8070; //port number
Socket server = null;
try
{
// Create a socket to accept client connections
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Bind(new IPEndPoint(IPAddress.Any, servPort));
server.Listen(BACKLOG);
}
catch (SocketException se)
{
Console.WriteLine(se.ErrorCode + ": " + se.Message);
Environment.Exit(se.ErrorCode);
}
//for now the server socket is waiting for connections
byte[] rcvBuffer = new byte[BUFSIZE]; // definir buffer
int bytesRcvd = 0 ; // numero de bytes recebidos
for (; ; )
{ // Run forever, accepting and servicing connections
Socket client = null;
try
{
client = server.Accept(); // Get client connection
Console.Write("Handling client at " + client.RemoteEndPoint + " - ");
do {
bytesRcvd = client.Receive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None);
string s = Encoding.ASCII.GetString(rcvBuffer, 0, bytesRcvd);
}while (true);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
client.Close();
}
}
}
}
}
So the client send a http get from time to time, i want to see those messages,
for now for the first time this runs the string gets the http get, and because I am maintaining the socket open, I want to wait for the next message (do{}(while(true))).
it goes back to the client.receive, and stops.
after detecting another message from the client it enters in forever loop and the string is empty.
I want something similar to a chat, where the client send messages and the server display the message.
So after opening the socket, what should i do to get the message from the client, then display it , then wait for another message?
One other thing that is bugging me is the client http get:
this is the http get
GET /iclock/cdata?SN=2182682370001&options=all&pushver=2.1.2&language=80 HTTP/1.1
Host: 192.168.1.43:8070
User-Agent: iClock Proxy/1.09
Connection: close
Accept: * / *
What does it mean when it says connection close? do i need create a new socket every time the client want to send a request?
Try to figure it out, i did some changes on the code, inside the do while i have got this
while ((bytesRcvd = client.Receive(rcvBuffer/*, 0, rcvBuffer.Length, SocketFlags.None*/)) > 0)
{
totalBytesEchoed += bytesRcvd;
string s = Encoding.ASCII.GetString(rcvBuffer, 0, bytesRcvd);
Console.WriteLine(s);
}
Image:
so in the image i have got wireshark running and my console app.
according to wireshark the client had tried to send 2 http request, but according to the console it only displayed one...
i want to display the two calls..
a few years ago, i did some exercises about sockets in c language.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void main(void)
{
struct sockaddr_in me, from;
int newSock, sock = socket(AF_INET, SOCK_STREAM, 0);
int adl = sizeof(me);
char linha[81];
bzero((char *) &me, adl);
me.sin_family = AF_INET;
me.sin_addr.s_addr = htonl(INADDR_ANY ); /* endereco IP local */
me.sin_port = htons(8450); /* porta local */
if (-1 == bind(sock, (struct sockaddr *) &me, adl))
{
close(sock);
puts("Porta ocupada");
exit(1);
}
listen(sock, 5);
newSock = accept(sock, (struct sockaddr *) &from, &adl);
close(sock);
do
{
read(newSock, linha, 81);
puts(linha);
} while (strcmp(linha, "exit"));
close(newSock);
}
this is in c, so as you can see,after the accept the socket stays open, and the client send the message, it only close the socket when the user send exit to the server...
there may be times where the client doesnt send...but the socket is still open, and when it detects
this is an example of what i want to achive, using the system.net.sockets library,,,
thanks
There are two kinds of sockets: The socket that you use to listen (it is never connected) and the sockets that correspond to connections (each socket represents one connection).
Accept returns you a connected socket to the client that was just accepted. Each call to Accept accepts a new , independent client.
If you want to handle more than one client at a time (which is almost always required) you must ensure that a call to Accept is pending at all times so that new clients can be accepted.
A simple model to achieve this is to accept in a loop forever and start a thread for each client that you accepted:
while (true) {
var clientSocket = listeningSocket.Accept();
Task.Factory.StartNew(() => HandleClient(clientSocket));
}
It might very well be that the client closes the connection.
To detect this on the server side you need to test the result of the call to Receive() against 0.
From MSDN's documentation on Receive() (emphasis by me):
If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the number of bytes specified by the size parameter. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.
From my experience, every time client disconnects from server/listener, this will be registrered on server as SocketException - client has forcibly disconnected. What I do at the moment is just try to catch this and "nicelly" ignore it (log/display some sort of message or run an action to inform the user about this).
Have a look at your code, slightly refactored. Try to run it and see what is happening. I have added some comments to explain a bit. Hope this will help.
There is also class called TcpListenner in .NET you can use to implement behavior you require.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace SimpleListener
{
class Program
{
const int PORT = 8070;
const int BACKLOG = 5;
static Socket socket;
private static byte[] rcvBuffer;
private static int bytesRcvd;
private static string message;
static void Main(string[] args)
{
try
{
StartListener();
Listen();
}
catch (Exception ex)
{
ShowMessage(ex.Message);
}
}
private static void StartListener()
{
try
{
//Initialize socket and start listenning
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Any, PORT));
socket.Listen(BACKLOG);
ShowMessage("I'm listenning now..");
}
catch (Exception)
{
throw;
}
}
private static void Listen()
{
try
{
rcvBuffer = new Byte[256];
bytesRcvd = 0;
message = string.Empty;
//Start listnening/waiting for client to connect
while (true)
{
var client = socket.Accept();
ShowMessage("Client with IP " + client.RemoteEndPoint.ToString() + " connected!");
//Client has connected, keep receiving/displaying data
while (true)
{
SocketError rcvErrorCode;
bytesRcvd = 0;
message = string.Empty;
bytesRcvd = client.Receive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None);
if (rcvErrorCode != SocketError.Success)
{
Console.WriteLine("Client with IP " + client.RemoteEndPoint.ToString() + " disconnected!");
break;
}
message = Encoding.ASCII.GetString(rcvBuffer, 0, bytesRcvd);
Console.WriteLine(DateTime.Now.ToLongTimeString() + " - " + message);
}
}
}
catch (Exception)
{
throw;
}
}
private static void ShowMessage(string message)
{
Console.WriteLine(message);
Console.WriteLine();
}
}
}