Reading TCP stream with PHP (converting C# code) - c#

I'm having problems getting/reading any response from a TCP server.
The connection is establishing fine and I'm writing to the server OK but there could be an issue with the particular EOT character required by this server (0x04).
Also the example code I've been given (see below - in C#) converts the string to binary/bytes, which from what I've read PHP doesn't require when using fsockopen & fputs.
My code:
$host = "xxx.xxx.xxx.xxx";
$port = 23432;
$msg = "sample message". chr(4);
$fp = fsockopen ($host, $port, $errno, $errstr);
if (!$fp) {
$lines = "Error: could not open socket connection";
} else {
// write the user string to the socket
fputs ($fp, $msg);
// get the result
$lines .= fgets($fp, 1024);
// close the connection
fclose($fp);
} ?>
Server said: <b><?php echo $lines; ?>
The example C# code I was given:
TcpClient tcp = new TcpClient();
tcp.Connect("xxx.xxx.xxx.xxx", 23432);
Console.WriteLine();
Console.WriteLine("Connected to tcp server.");
// Sent request to tcp server
NetworkStream stream = tcp.GetStream();
// Ask for travel time data
byte[] tx = GetBytes("sample message");
stream.Write(tx, 0, tx.Length);
// Receive data from tcp server
byte[] buffer = new byte[0xffff];
int bytesRead = stream.Read(buffer, 0, 0xffff);
Console.WriteLine("Received " + bytesRead + " bytes from tcp server");
string s = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine("Below is the data received.");
Console.WriteLine(s);
// Keep the connection open, the data is coming every 20 secs
tcp.Close();
// This function will add the binary byte (0x04) to the end of the text
static private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length + 1];
Encoding.ASCII.GetBytes(str, 0, str.Length, bytes, 0);
bytes[str.Length] = 0x04;
return bytes;
}
Any ideas? At the moment the PHP code sits there for about 40 seconds after sending the message and returns nothing.

Related

Why does my encoding add \0 efter decoding?

So I am sending a simple string from my TCP Client to my server and then when I receive it it decodes the bytes and prints out what it got.. However I am sending
Client connecting..
and I receive
Client connecting..\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0
Why is that?
The buffer seems to be the same, 18 bytes with the empty ones being 0
is this due to the Encoding method? I've tried different ones likes Default and UTF8 but it seems to do the same still.
_listener.Start();
Console.WriteLine("Waiting for connection..");
//Assign our client the value of the first accepted request.
_client = _listener.AcceptTcpClient();
Console.WriteLine("Client connected.");
//Set the stream to listen for incoming requests.
_stream = _client.GetStream();
//Build the package
byte[] buffer = new byte[128];
var bufferLength = _stream.Read(buffer, 0, buffer.Length);
return buffer;
And it passes the byte array to this
public void SendPacket(byte[] buffer)
{
TcpClient client = new TcpClient(hostName, portNum);
NetworkStream ns = client.GetStream();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Received from remote client: {Encoding.UTF8.GetString(buffer, 0, buffer.Length)}");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Relaying to the remote server: {Encoding.UTF8.GetString(buffer, 0, buffer.Length)}");
ns.Write(buffer, 0, buffer.Length);
}
And then
Console.WriteLine("Data Received..");
var data = Encoding.UTF8.GetString(buffer, 0, length);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received: " + data);
According to your code
var bufferLength = _stream.Read(buffer, 0, buffer.Length);
return buffer;
bufferLength is dropped on the floor.
var data = Encoding.UTF8.GetString(buffer, 0, length);
I don't know where length came from, but I suppose it's the buffer's length. Since the receive didn't fill up the whole buffer but only part of it, you must use bufferLength to know how many bytes to work with.
Same problem here:
var eh = ns.Read(newBuffer, 0, newBuffer.Length);
var received = Encoding.ASCII.GetString(newBuffer, 0, newBuffer.Length);
Use eh!
var eh = ns.Read(newBuffer, 0, newBuffer.Length);
var received = Encoding.ASCII.GetString(newBuffer, 0, eh);
\0 is the "empty" value for a char object, so most likely it is reading the chars.

Send and Receive large amount of data through tcp stream using read/write (Error)

I am trying to receive data at my server of any length through tcp connection. First my client sends length of data to server through stream.write then it send the actual data.
At Client I receive the length and loop until whole the data is received successfully.
The problem is: "I receive 0 size on the server no matters what the length of data is". I tried to figure out the issue but could not get where the problem is. Any kind of help/hint would be appreciated.
Server Side Code:
byte[] lengthOfData = new byte[2048];
byte[] buffer;
try
{
stream = client.GetStream();
eventLog1.WriteEntry("Size of 1st = "+stream.Read(lengthOfData,0,lengthOfData.Length));
int numBytesToRead = ByteArraySerializer.BytesArrayToInt(lengthOfData);
eventLog1.WriteEntry("number of bytes to read= "+numBytesToRead);
buffer = new byte[numBytesToRead+10];
int numBytesRead = 0;
do
{
int n = stream.Read(buffer, numBytesRead, 10);
numBytesRead += n;
numBytesToRead -= n;
eventLog1.WriteEntry("number of bytes read= " + numBytesRead);
} while (numBytesToRead > 0);
}
catch (Exception e) // Called automatically when Client Diposes or disconnects unexpectedly
{
eventLog1.WriteEntry("Connection Closes: "+e.ToString());
lock (connectedClients)
{
connectedClients.Remove(client);
}
client.Close();
break;
}
Client Side Code
byte[] command = ByteArraySerializer.Serialize<Command>(cmd);
byte[] sizeOfData = ByteArraySerializer.IntToBytesArray(command.Length);
stream.Write(sizeOfData, 0, sizeOfData.Length);
Console.WriteLine("Size of Data = "+command.Length);
stream.Write(command, 0, command.Length);
Change the following line at server
byte[] lengthOfData = new byte[2048];
to
byte[] lengthOfData = new byte[sizeof(int)];
The issue was that the read at server was supposed to read only 4 bytes integer whereas it was reading other data as well which was getting written after writing the length of data. We are supposed to read only 4 bytes if we want to get the length of data(integer).

Need help on bytes via Socket

I'm trying to send a couple of data via Sockets, so it's converted to bytes and then back to String on the Server. But I can only do one apparently.
Server code:
static void Read(IAsyncResult ar)
{
int fileNameLen = 1;
//int userNameLen = 9;
State newState = (State)ar.AsyncState; //gets state of Socket
Socket handler = newState.Socket_w; //passes Socket to handler
int bytesRead = handler.EndReceive(ar); //terminates Data Receive from Socket.
if (bytesRead > 0)
{
if (flag == 0)
{
fileNameLen = BitConverter.ToInt32(newState.buffer, 0); //gets filename length
fileName = Encoding.UTF8.GetString(newState.buffer, 4, fileNameLen); //gets filename
//userNameLen = BitConverter.ToInt32(newState.buffer, 8);
//getUsername = Encoding.UTF8.GetString(newState.buffer, 8, fileNameLen);
flag++;
}
}
}
Client code:
internal static void uploadFile(string host, string username, string getGame, string filename, string filepath)
{
byte[] m_clientData;
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] fileName = Encoding.UTF8.GetBytes(username + "_" + filename);
byte[] fileData = File.ReadAllBytes(filepath);
byte[] fileNameLen = BitConverter.GetBytes(fileName.Length);
//byte[] sendUsername = Encoding.UTF8.GetBytes(username);
//byte[] sendUsernameLen = BitConverter.GetBytes(sendUsername.Length);
//byte[] sendGame = Encoding.UTF8.GetBytes(getGame);
//byte[] sendGameLen = BitConverter.GetBytes(sendGame.Length);
m_clientData = new byte[4 + fileName.Length + fileData.Length];
fileNameLen.CopyTo(m_clientData, 0);
fileName.CopyTo(m_clientData, 4);
fileData.CopyTo(m_clientData, 4 + fileName.Length);
//sendUsernameLen.CopyTo(m_clientData, 0);
//sendUsername.CopyTo(m_clientData, 4);
//sendGameLen.CopyTo(m_clientData, 0);
//sendGame.CopyTo(m_clientData, 4);
clientSock.Connect(host, 8889);
clientSock.Send(m_clientData); //tofix exception
clientSock.Close();
}
I can't seem to decrypt it properly over on Server. Can anyone help me with the buffersizes and whatnot?
Read does not know anything about what was sent; TCP is basically just a stream - so there is absolutely nothing to say that you have all the data in one call to Read; you could have:
exactly the amount of data you sent
part of one message
17 messages
the end of one message and the start of the next
1 solitary byte from a message
You need to devise some kind of framing protocol that lets the receiver know when they have an entire message. That could be as simple as a length prefix, or can be more complex. You should then buffer the data in memory (or process it gradually) until you have the entire message. One call to Read is very unlikely to represent a single and complete message. Indeed, this is guaranteed not to be the case if a message is larger than your newstate.buffer, but you can get the same result even for small messages and a large buffer.

Send File From Java to C# using Socket

Can anyone give me a small tutorial on how to send file from java server to c# client and on receive complete acknowledgment message from c# to java. Actually I'm new to C# and dont know how to do socket programming. I'm stuck in it since long. Tried many codes. Some codes receive incomplete files some stuck in infinite loop. Please help me in this regard.
Thanks
EDIT
Here is what I have tried:
C# Server:
{
IPAddress ipAd = IPAddress.Parse("192.168.1.131");
// use local m/c IP address, and
// use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 5600);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port 5600...");
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
m:
clientSock = myList.AcceptSocket();
//clientSock.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,10000);
Console.WriteLine("Connection accepted from " + clientSock.RemoteEndPoint);
//byte[] b = new byte[100];
//int k = clientSock.Receive(b);
string fileName = "hello.wav";
NetworkStream networkStream = new NetworkStream(clientSock);
StreamReader sr = new StreamReader(networkStream);
//read file length
int length = int.Parse(sr.ReadLine());
if (networkStream.CanRead)
{
BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Create));
int receivedBytesLen = -1;
byte[] clientData = new byte[4096 * 5000];
receivedBytesLen = networkStream.Read(clientData, 0, clientData.Length);
bWrite.Write(clientData, 0, receivedBytesLen);
do
{
receivedBytesLen = networkStream.Read(clientData, 0,clientData .Length);
bWrite.Write(clientData, 0, receivedBytesLen);
} while (receivedBytesLen > 0);
bWrite.Close();
networkStream.Close();
}
Console.WriteLine("Client:{0} connected & File {1} started received.", clientSock.RemoteEndPoint, fileName);
Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath);
Recognizer_2 recognizeVoice = new Recognizer_2(clientSock);
recognizeVoice.recognize_wav(); // Acknowledgement
Console.WriteLine("\nResult Sent to the Client");
goto m;
}
Java Client:
Socket socket = new Socket("192.168.1.131", 5600);
BufferedReader response_Stream = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
File f = new File(mFileName);
byte[] buffer = new byte[(int) f.length()];
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(buffer, 0, buffer.length);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(buffer);
outputStream.flush();
String final_Result_String = "";
if (response_Stream != null) {
String respose_text = "";
while ((respose_text = response_Stream.readLine()) != null) {
final_Result_String += respose_text;
}
}
Toast.makeText(getApplicationContext(), final_Result_String, 1)
.show();
outputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
There is no dependance between the languages used by the server or the client.
Just the structure of the data is important !
You should search for some tutorials on socket programming with C#.
For example: http://www.codeproject.com/Articles/10649/An-Introduction-to-Socket-Programming-in-NET-using
But the language doesn't matter, understand how the data is formatted when sent on the network.
Edit: you should add a byte or two in the data indicating the length of it. It's not because you dont have data to read once that all the data has been received.

TCPClient. How do I receive big messages?

I have the following code:
private string Connect()
{
string responseData;
try
{
TcpClient client = new TcpClient(ServerIp, Port);
client.ReceiveBufferSize = Int32.MaxValue;
Byte[] data = Encoding.GetEncoding(1251).GetBytes(ReadyQuery);
NetworkStream stream = client.GetStream();
// send data
stream.Write(data, 0, data.Length);
// buffer
data = new Byte[65536];
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = Encoding.GetEncoding(1251).GetString(data, 0, bytes);
// close all
stream.Close();
client.Close();
return responseData;
}
I have problem with a big message. The receive message size is 22K chars. I get only part of message.
How can I receive big messages?
PS. In the debugger bytes equal 4096.
You call stream.Read in a loop until you read the entire message. If you know the message size in advance it's relatively easy:
int messageSize = 22000;
int readSoFar = 0;
byte [] msg = new byte[messageSize];
while(readSoFar < messageSize)
{
var read = stream.Read(msg, readSoFar, msg.Length - readSoFar);
readSoFar += read;
if(read==0)
break; // connection was broken
}
If the message size is part of the message (say, encoded in the first 4 bytes), you should read those first and then do as I suggested.

Categories

Resources