I'm trying to collect data from a IP Address and Port using a TCP Client or a socket, but can't seem to find a way to successfully write what I receive to either the Console or to a file. Of the numerous sources I've sifted through online including MSDN documentation and various blogs, I found this one to be the most understandable, yet it still doesn't write anything to the console and I know the IP Address / Port (which I can't share) is supposed to be sending me a stream of data.
What am I doing wrong?
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace TCPIPChallenge
{
public class Program
{
public static void Main(string[] args)
{
Client client = new Client();
client.SetupServer();
}
}
public class Client
{
private Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private byte[] _recieveBuffer = new byte[8142];
private void SetupServer()
{
try
{
_clientSocket.Connect(new IPEndPoint(IPAddress.Parse("0.0.0.0"), 8888));
Console.WriteLine("It was successful!");
}
catch (SocketException ex)
{
Console.WriteLine("There was an issue...");
Debug.Write(ex.Message);
}
Console.WriteLine(_clientSocket.Connected);
_clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
private void ReceiveCallback(IAsyncResult AR)
{
//Check how much bytes are recieved and call EndRecieve to finalize handshake
int recieved = _clientSocket.EndReceive(AR);
Console.WriteLine(_clientSocket.Connected);
if (recieved <= 0)
return;
//Copy the recieved data into new buffer , to avoid null bytes
byte[] recData = new byte[recieved];
Buffer.BlockCopy(_recieveBuffer, 0, recData, 0, recieved);
//Process data here the way you want , all your bytes will be stored in recData
Console.WriteLine(recData.ToString());
//Start receiving again
_clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
}
}
It seems you are not waiting for the result to be displayed. Put a Console.Read(); in your Main.
public static void Main(string[] args)
{
Client client = new Client();
client.SetupServer();
Console.Read();
}
This seems like the suspect here:
Console.WriteLine(recData.ToString());
You can't just call ToString
on a byte array expecting it to turn into a string because there are many different types of encoding that can convert bytes into string and vice versa. Some encoding takes more bytes than others, like UTF-8 and UTF-16 for example.
Example using Unicode encoding
Converting your message to bytes:
byte[] data = System.Text.Encoding.Unicode.GetBytes("Hello!");
Displaying the data again:
byte[] recData = new byte[recieved];
string message = System.Text.Encoding.Unicode.GetString(recData);
Console.WriteLine(message); // "Hello!"
Ensure that you use similar encoding on both server and client side to get consistant data.
Different encoding types: UTF-8, UTF-16, and UTF-32
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.