This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NullReferenceException on instanciated object?
What is a NullReferenceException in .NET?
I have built a TCP client in C#, when the client cannot connect Im getting the below.
I keep getting a NullReference Exception with the following code, I cannot figure how to catch it or stop it.. any help will be greatly appreciated.
Its happening at "int a = sReponseData.Length"
{
string sMesg = "LogOn Ext:" + txtdevice.Text;
SendMessage(sMesg);
int a = sResponseData.Length;
//Geting the Script out of the message.
if (a > 10)
{
string stemp = sResponseData;
string[] sSplit = stemp.Split(new Char[] { ';'});
foreach (string s in sSplit)
{
if ((s.Trim() != "Tag") & (s.Trim() != "StopStart"))
sScript = s;
}
}
}
}
and here is the sMesg
{
InitializeComponent();
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
//Int32 port = 13000;
TcpClient client = new TcpClient("127.0.0.1", 32111);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(sMsg);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
//MessageBox.Show("Sent: {0}" + sMsg);
// Receive the TcpServer.response.
// String to store the response ASCII representation.
sResponseData = String.Empty;
if (stream.CanRead)
{
byte[] myReadBuffer = new byte[1024];
StringBuilder myCompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;
// Incoming message may be larger than the buffer size.
do
{
numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
}
while (stream.DataAvailable);
// Print out the received message to the console.
sResponseData = myCompleteMessage.ToString();
//MessageBox.Show(sResponseData);
}
else
{
sResponseData = ("3");
MessageBox.Show("TagIt Server is unavalible at this moment.");
}
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
WriteLog("ArgumentNullException: {0}" + e);
MessageBox.Show("TagIt Server is unavalible at this moment.");
}
catch (SocketException e)
{
WriteLog("SocketException: {0}" + e);
MessageBox.Show("TagIt Server is unavalible at this moment.");
}
}
You can check if Response data has value or not:
Something like:
int a;
try
{
if(sResponseData==null || sResponseData=="" )
{
MessageBox.Show("ResponseData is NULL or Empty"); //Shows Error
}
else
{
//SresponseData has value
string sMesg = "LogOn Ext:" + txtdevice.Text;
SendMessage(sMesg);
a= Convert.ToInt32(sResponseData).Length;
//Geting the Script out of the message.
if (a > 10)
{
string stemp = sResponseData;
string[] sSplit = stemp.Split(new Char[] { ';'});
foreach (string s in sSplit)
{
if ((s.Trim() != "Tag") & (s.Trim() != "StopStart"))
sScript = s;
}
}
}
}
catch (Execption ex)
{
MessageBox.Show(ex.Message); //Shows Error
}
Before checking length of sResponseData make sure you check whether sResponseData is null or not.
Related
I might have a pretty basic question for some of you.
I have created a one(server) to many(clients) relationship where in which server selects a client to send and receive data from and to.
My question is, how can I distinguish incoming data from the different clients? Would I have to simply indicate this in a buffer?
Currently My server listens, accepts and adds clients to a list clientList:
private void StartListen()
{
//Creating a TCP Connection and listening to the port
tcpListener = new TcpListener(System.Net.IPAddress.Any, 6666);
tcpListener.Start();
toolStripStatusLabel1.Text = "Listening on port 6666 ...";
int counter = 0;
appStatus = 0;
while (true)
{
try
{
client = tcpListener.AcceptTcpClient();
counter++;
clientList.Add(client);
IPEndPoint ipend = (IPEndPoint)client.Client.RemoteEndPoint;
//Updating status of connection
toolStripStatusLabel1.Text = "Connected from " + IPAddress.Parse(ipend.Address.ToString());
appStatus = 1;
th_inPutStream = new Thread(delegate () { inPutStream(client); });
th_inPutStream.Start();
th_checkConnection = new Thread(checkConnection);
th_checkConnection.Start();
}
catch (Exception err)
{
Cleanup_dep();
}
}
}
Now i assume if I want to send a message to a specific client, id select the client by clientList[i] and send data.
My question is, when data is available in the network stream to read, how can i know which client its coming from?
This is how I manage incoming data:
private void inPutStream(TcpClient client)
{
try
{
while (true)
{
NetworkStream networkstream = client.GetStream();
if (networkstream.DataAvailable == true)
{
int messageID = 0;
int messageSize = 0;
int bufferSize = 100;
//First retrieve size of header
byte[] fileSizeBytes = new byte[4];
int bytes = networkstream.Read(fileSizeBytes, 0, 4);
int dataLength = BitConverter.ToInt32(fileSizeBytes, 0);
//Read stream containing header with message details
byte[] header = new byte[dataLength];
int headerbyte = networkstream.Read(header, 0, dataLength);
string headerString = Encoding.ASCII.GetString(header, 0, headerbyte);
// Process Message information ie Message ID & Message Size
string[] headerSplit = headerString.Split(new string[] { "\r\n" }, StringSplitOptions.None);
Dictionary<string, string> headers = new Dictionary<string, string>();
foreach (string s in headerSplit)
{
if (s.Contains(":"))
{
headers.Add(s.Substring(0, s.IndexOf(":")), s.Substring(s.IndexOf(":") + 1));
}
}
//Fetch Message ID & Size
messageSize = Convert.ToInt32(headers["len"]);
messageID = Convert.ToInt32(headers["MsgId"]);
//Filter actions by Message ID
if (messageID == 1)//Machine info
{
string machineinfo = processMessage(messageSize, bufferSize, networkstream);
clientNames.Add(machineinfo.Split(new char[] { '\r', '\n' })[0]);
updateClientListUI();
}
if (messageID == 2)//CMD Commands
{
updateText(txtCmdConsole, processMessage(messageSize, bufferSize, networkstream));
}
}
}
}
catch (Exception err)
{
{
Cleanup_dep();
}
}
}
So if networkstream.DataAvailable == true then it reads, but how can i know from which stream?
When trying to receive a string or file content (as string) via TCP I am stuck with an issue wher the receiving works in general, but the line
print("TCP -> Data received:\n" + file + "\n\n" + totalrecbytes + " Bytes");
is kind of stalled until I activelly disconnect from the server side. Than it works as expected.
I debugged and receiving the data inside the
while ((recBytes = netstream.Read(bytes, 0, bytes.Length)) > 0)
loop works just fine. It also ends the loop in the correct moment. But after that simply nothing happens. I get no errors, am not "trapped" in any loop but also do not get the expected output of
print("TCP -> Data received:\n" + file + "\n\n" + totalrecbytes + " Bytes");
until I disconnect from the server side. Than I see the expected output and the client is disconnected.
Here is the implementation (original source)
private Thread _tcpThread;
private TcpClient _socketConnection;
public void Connect()
{
try
{
_tcpThread = new Thread(ReciveDataClient);
_tcpThread.IsBackground = true;
_tcpThread.Start();
}
catch (Exception e)
{
print(e.Message);
}
}
private void ReciveDataClient()
{
try
{
_socketConnection = new TcpClient("xxx.xxx.xxx.xxx", 54321);
print(this, "TCP -> Connection Success!");
}
catch (Exception e)
{
print("connection error: " + e.Message)
return;
}
try
{
var bytes = new byte[BUFFER_SIZE];
while (_socketConnection.Connected)
{
if (_socketConnection.Available <= 0) continue;
// Get a stream object for reading
var netstream = _socketConnection.GetStream();
int totalrecbytes = 0;
int recBytes;
string file = "";
// Read incomming stream into byte arrary.
while ((recBytes = netstream.Read(bytes, 0, bytes.Length)) > 0)
{
var incommingData = new byte[recBytes];
Array.Copy(bytes, 0, incommingData, 0, recBytes);
// Convert byte array to string message.
var serverMessage = Encoding.ASCII.GetString(incommingData);
file += serverMessage;
totalrecbytes += recBytes;
}
print("TCP -> Data received:\n" + file + "\n\n" + totalrecbytes + " Bytes");
netstream.Close();
}
print("TCP -> connection was terminated by the server");
}
catch (Exception e)
{
print(e.Message)
return;
}
}
I would expect that I can maintain the connection alive but still receive the data correctly and communicate with the server on a persistent TCP connection.
What am I missing or doing wrong here?
The only workarround I could find so far is allways disconnect from the server side after sending data and in my code wrap the whole ReceiveDataClient in a while loop like
private void ReciveDataClient()
{
while (true)
{
try
{
_socketConnection = new TcpClient(_server.ToString(), _server.Port);
//...
in order to immediately start a new connection after the server sent some data and disconnected the client.
Thanks to the help of Damien_The_Unbeliever and Immersive I could figure it out. It really helps to read the docs from time to time especially if it is the first time you use something ^^
NetworkStream.Read is a blocking call and as the doc states
returns: The number of bytes read from the NetworkStream, or 0 if the socket is closed.
so ofcourse the while loop actually never terminated.
So adopting the example provided there worked for me except that if the server ended the connection I got another issue so instead of checking for _socketConnection.IsConnected I used the marked answer from this post so all together this works for me now
private Thread _tcpThread;
private TcpClient _socketConnection;
public void Connect()
{
if(_socketConnection != null) return;
try
{
_tcpThread = new Thread(ReciveDataClient);
_tcpThread.IsBackground = true;
_tcpThread.Start();
}
catch (Exception e)
{
print("TCP -> Thread error: " + e.Message);
}
}
public void Disconnect()
{
if(_socketConnection = null) return;
_tcpThread.Abort();
}
private void ReciveDataClient()
{
try
{
_socketConnection = new TcpClient("xxx.xxx.xxx.xxx", 54321);
print(this, "TCP -> Connection Success!");
}
catch (Exception e)
{
print("TCP -> connection error: " + e.Message)
return;
}
try
{
while(true)
{
// Get a stream object for reading
var netstream = _socketConnection.GetStream();
//Check if still connected
if(_socketConnection.Client.Poll(0, SelectMode.SelectRead))
{
byte[] buff = new byte[1];
if( _socketConnection.Client.Receive( buff, SocketFlags.Peek ) == 0 )
{
// Server disconnected or connection lost
break;
}
}
// Check to see if this NetworkStream is readable.
if(myNetworkStream.CanRead)
{
byte[] myReadBuffer = new byte[BUFFER_SIZE];
StringBuilder myCompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;
int totalBytesReceived = 0;
// Incoming message may be larger than the buffer size.
do
{
numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
totalBytesReceived += numberOfBytesRead;
}
while(myNetworkStream.DataAvailable);
// Print out the received message to the console.
print("TCP -> Data received:\n" + myCompleteMessage.ToString() + "\n\n" + totalrecbytes + " Bytes");
}
else
{
//Prevent a direct loop
Thread.Sleep(100);
}
}
print("TCP -> connection was terminated by the server");
}
catch(ThreadAbortException)
{
print("TCP -> Disconnected");
}
catch(Exception e)
{
print(e.Message)
}
// Clean up
_socketConnection?.Close();
_socketConnection?.Dispose();
_socketConnection = null;
}
i have the following code executed by a listening Thread. What it does: Reading the first Message as the total message length then assemble all packets to a big data array. (Im sending images) It all works as intended.
But after the first image is recieved and the function is done. ("ImageLengthResetted" gets printed) It closes the connection. I think this is due the fact i am running out of the scope from:
using(connectedTcpClient = tcpListener.AcceptTcpClient())
and thats what kills the connection. How can i keep this connection open?
Adding another
while(true)
after i've been connected wont do the trick. As well as executing the while loop completle after the using statments.
private void ListenForIncommingRequests()
{
try
{
// Create listener on localhost port 8052.
localAddr = IPAddress.Parse(IPadrr);
Debug.Log(localAddr);
tcpListener = new TcpListener(localAddr, port);
Debug.Log("Before Init tcplistern");
tcpListener.Start();
Debug.Log("Server is listening");
Byte[] dataRecieved = new Byte[SEND_RECIEVE_COUNT];
while (true)
{
using (connectedTcpClient = tcpListener.AcceptTcpClient())
{
Debug.Log("Accepted TCP Client");
// Get a stream object for reading
using (NetworkStream stream = connectedTcpClient.GetStream())
{
int length;
Debug.Log("Accepted Stream");
// Read incomming stream into byte arrary.
while ((length = stream.Read(dataRecieved, 0, dataRecieved.Length)) != 0)
{
Debug.Log("receiving Loop lengt: " + length);
counterReceived++;
//Get Message length with first message
if (messageLength == 0)
{
messageLength = System.BitConverter.ToInt32(dataRecieved, 0);
data = new byte[messageLength];
messageJunks = messageLength / SEND_RECIEVE_COUNT;
restMessage = messageLength % SEND_RECIEVE_COUNT;
junkCounter = 0;
}
else
{
if (junkCounter < messageJunks)
{
Array.Copy(dataRecieved, 0, data, junkCounter * SEND_RECIEVE_COUNT, SEND_RECIEVE_COUNT);
junkCounter++;
}
else
{
Array.Copy(dataRecieved, 0, data, junkCounter * SEND_RECIEVE_COUNT, restMessage);
//Whole Message recieved, reset Message length
messageLength = 0;
readyToDisplay = true;
Debug.Log("ImageLengthResetteed");
}
}
}
}
}
}
}
catch (Exception socketException)
{
Debug.Log("SocketException " + socketException.ToString());
}
}
Client Side opens send Thread with following function where socketConnection gets globally initialized on the receiving thread of the client:
private void sendData(byte[] data)
{
try
{
//socketConnection = new TcpClient(IPadrr, port);
using (NetworkStream stream = socketConnection.GetStream())
{
//Prepare the Length Array and send first
byte[] dataLength = BitConverter.GetBytes(data.Length);
int packagesSend = 0;
int numberPackages = data.Length / SEND_RECIEVE_COUNT;
if (stream.CanWrite)
{
for (counter = 0; counter <= data.Length; counter += SEND_RECIEVE_COUNT)
{
if (counter == 0)
{
stream.Write(dataLength, 0, dataLength.Length);
}
if (packagesSend < numberPackages)
{
stream.Write(data, counter, SEND_RECIEVE_COUNT);
Thread.Sleep(20);
packagesSend++;
}
else
{
stream.Write(data, counter, data.Length % SEND_RECIEVE_COUNT);
Debug.Log("FINDISCHD");
}
}
}
}
}
catch (Exception err)
{
print(err.ToString());
}
}
Im glad for any help!
The Problem was on the Client Side. I initialized the
NetworkStream stream;
now globally in the same function the socketConnection gets init.
I am trying to implement a system that can send and receive "Nessages" (a custom type) using async Sockets. Each Message consists of three elements:
First I send the Message-ID (4 bytes), next is the length of the data (4 bytes) and then the actual data.
The way TCP/IP works, I can not know if the full Message is in the buffer of data that I just received. I need to look for the InfoHeader (ID and length) first and use the remaining data to fill the Message's DataBuffer.
public void ReadCallback(IAsyncResult ar)
{
try
{
// How many bytes were received? They are now stored in the PacketBuffer
int bytesReceived = Socket.EndReceive(ar);
if (bytesReceived != 0)
{
// We did receive some data
if (MessageInfoHeader.Length < NetProtocol.NetInfoHeaderSize)
{
// The InfoHeader we have is not complete yet. How many bytes are missing from it?
int infoHeaderDataInCurrentBuffer = NetProtocol.NetInfoHeaderSize - (int) MessageInfoHeader.Length;
// Check if we have read enough data to fill the entire missing InfoHeader
if (infoHeaderDataInCurrentBuffer > bytesReceived)
{
// Nope, we dont. Fill as much as we can
infoHeaderDataInCurrentBuffer = bytesReceived;
Log.Info("We do not have enough data to complete the InfoHeader yet. Fill as much as we can");
}
else
{
Log.Info("We do have enough data to complete the InfoHeader");
}
// Now fill it from the chunk of data we just received
MessageInfoHeader.Write(ReceivedData, 0, infoHeaderDataInCurrentBuffer);
// See if there is any data remaining in the buffer that we can store in the MessageDataBuffer
int dataRemainingInPacket = bytesReceived - infoHeaderDataInCurrentBuffer;
Log.Info("DataRemainingInPacket: " + dataRemainingInPacket);
// Check again if we have the full header
if (MessageInfoHeader.Length == NetProtocol.NetInfoHeaderSize)
{
Log.Info("We now have assembled the full InfoHeader");
// We do have the full InfoHeader. Use the rest of the data to fill the DataBuffer
using (MemoryStream ms = new MemoryStream())
{
byte[] b = MessageInfoHeader.GetBuffer();
ms.Write(b, 0, b.Length);
ms.Seek(0, SeekOrigin.Begin);
try
{
using (BinaryReader br = new BinaryReader(ms))
{
CurrentMessageID = br.ReadInt32();
CurrentMessageDataLength = br.ReadInt32();
Log.Info("InfoHeader: MessageID=" + CurrentMessageID + " / DataLength=" + CurrentMessageDataLength);
}
}
catch (Exception ex)
{
throw ex;
}
}
if (dataRemainingInPacket > 0)
{
Log.Info("There are " + dataRemainingInPacket + " bytes remaining in the packet. Writing them to the PacketDataBuffer");
MessageDataBuffer.Write(ReceivedData, CurrentPacketBytesRead, ReceivedData.Length - CurrentPacketBytesRead);
}
}
}
else
{
MessageDataBuffer.Write(ReceivedData, 0, ReceivedData.Length);
if(ReceivedData.Length <= NetDataBufferSize)
{
Log.Info("WE HAVE RECEIVED THE ENTIRE MESSAGE");
}
}
// Start receiving the new TCP-Packet
Socket.BeginReceive(ReceivedData, 0, ReceivedData.Length, SocketFlags.None, new AsyncCallback(ReadCallback), null);
}
else
{
Log.Info("No data has been received");
}
}
catch (Exception ex)
{
throw ex;
}
}
private void AcceptCallback(IAsyncResult ar)
{
// Retrieve the listening Socket
Socket listener = (Socket)ar.AsyncState;
// Retrieve the Socket that is actually connected to the client
Socket csocket = listener.EndAccept(ar);
Log.Info("Accepted connection from: " + csocket.RemoteEndPoint.ToString());
// Create a new client with the CSocket and add it to the list
ClientConnection client = ClientManager.AddClient(csocket);
// Start reading data from the Client's Socket and then enter the "read-loop" inside the client
client.Socket.BeginReceive(client.ReceivedData, 0, (int) client.ReceivedData.Length, SocketFlags.None, new AsyncCallback(client.ReadCallback), null);
// Turn the listener back into listening mode
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
}
public const int NetInfoHeaderSize = 8; // 2x int
public static byte[] FrameMessage(NetMessage message)
{
byte[] result;
using(MemoryStream ms = new MemoryStream())
{
using (BinaryWriter bw = new BinaryWriter(ms))
{
bw.Write(message.ID);
bw.Write(message.Data.Length);
bw.Write(message.Data);
}
result = ms.ToArray();
}
return result;
}
This piece of code obviously does not work correctly. I get the "RECEIVED ENTIRE MESSAGE" line multiple times.
Question: What exactly am I doing wrong? I'm failing to wrap my head around what is the best way to handle something like this? Thanks in advance for any input!
There could be quite a few problems. But the first one I can see for the loop on "RECEIVED ENTIRE MESSAGE" is that you are assuming the ReceivedData buffer has been filled entirely by Socket.EndReceive(ar); - which isn't the case, so a first troubleshooting start could be to rewrite this:
MessageDataBuffer.Write(ReceivedData, 0, ReceivedData.Length);
if (ReceivedData.Length <= NetDataBufferSize)
{
Log.Info("WE HAVE RECEIVED THE ENTIRE MESSAGE");
}
to:
MessageDataBuffer.Write(ReceivedData, 0, bytesReceived);
if (MessageDataBuffer.Length <= NetDataBufferSize)
{
Log.Info("WE HAVE RECEIVED THE ENTIRE MESSAGE");
}
Assuming that MessageDataBuffer is intended to contain the entire message
In C# to test if UltraVNC was up and running on a local machine I would do this
public static bool TestAvailablility(int port, string responseStartsWith)
{
bool toReturn = false;
try
{
using (TcpClient client1 = new TcpClient())
{
client1.ReceiveTimeout = 10000;
client1.SendTimeout = 10000;
client1.Connect("localhost", port);
using (NetworkStream stream = client1.GetStream())
{
Byte[] response = new Byte[4096];
Int32 bytes = 0;
string serverReturnString = null;
bytes = stream.Read(response, 0, response.Length);
serverReturnString = System.Text.Encoding.ASCII.GetString(response, 0, bytes);
Console.WriteLine("TestAvailablility: serverReturnString = {0}", serverReturnString);
if (serverReturnString.StartsWith(responseStartsWith, StringComparison.OrdinalIgnoreCase))
{
toReturn = true;
}
}
}
}
catch (Exception ex) // SocketException for connect, IOException for the read.
{
Console.WriteLine("TestAvailable - Could not connect to VNC server. Exception info: ", ex);
}
return toReturn;
}
I'm new to Java so I'm hoping someone can help me with an equivalent method to preform this action.
Here's what I came up with:
FYI using Autocomplete in Eclipse + Java API you can translate C# to Java pretty easily.
public static boolean testAvailablility(int port, String responseStartsWith) {
boolean toReturn = false;
try {
Socket client1 = new Socket();
client1.setSoTimeout(10000);
client1.bind(new InetSocketAddress("localhost", port));
InputStream stream = client1.getInputStream();
byte[] response = new byte[4096];
int bytes = 0;
String serverReturnString = null;
bytes = stream.read(response, 0, response.length);
serverReturnString = String.valueOf(bytes);
System.out.println("TestAvailablility: serverReturnString = {0} " + serverReturnString);
if (serverReturnString.toLowerCase().startsWith(responseStartsWith.toLowerCase()))
toReturn = true;
} catch (Exception ex) // SocketException for connect, IOException for
// the read.
{
System.out.println("TestAvailable - Could not connect to VNC server. Exception info: ");
ex.printStackTrace();
}
return toReturn;
}