I have a socket server and am trying to receive a string from the client.
The client is perfect and when I use this
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine(k);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++) {
Console.Write(Convert.ToChar(b[i]));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
}
All is okay and I get my string in the console.
But how can I get now my receive into a string so I can use it in a switch case?
Like this:
string action = Convert.ToChar(b[i]);
Error:
The Name i isn't in the current context.
its the only Error Message i get.
This way no need set buffer size, it fits to response:
public static byte[] ReceiveAll(this Socket socket)
{
var buffer = new List<byte>();
while (socket.Available > 0)
{
var currByte = new Byte[1];
var byteCounter = socket.Receive(currByte, currByte.Length, SocketFlags.None);
if (byteCounter.Equals(1))
{
buffer.Add(currByte[0]);
}
}
return buffer.ToArray();
}
Assuming s is a Socket object on which you call receive, you get an byte[] back. To convert this back to a string, use the appropiate encoding, e.g.
string szReceived = Encoding.ASCII.GetString(b);
Edit: Since the buffer b is always 100 bytes, but the actual number of bytes received varies with each connection, one should use the return value of the Socket.Receive() call to only convert the actual number of received bytes.
byte[] b = new byte[100];
int k = s.Receive(b);
string szReceived = Encoding.ASCII.GetString(b,0,k);
Init socket
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ipAdd = System.Net.IPAddress.Parse(m_ip);
IPEndPoint remoteEP = new IPEndPoint(ipAdd, m_port);
Connect socket
socket.Connect(remoteEP);
Receive from socket
byte[] buffer = new byte[1024];
int iRx = socket.Receive(buffer);
char[] chars = new char[iRx];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String recv = new System.String(chars);
Send message
byte[] byData = System.Text.Encoding.ASCII.GetBytes("Message");
socket.Send(byData);
Close socket
socket.Disconnect(false);
socket.Close();
Related
I have C# Client and C# Server Programs that connected with each other successfully and works fine. But when i want to connect C# Client with C++ server then C# client gets halted while C++ Server produces success messages of "winsock initialization success " and "creating socket success".
C++ Server Code
WSADATA wsaData;
struct sockaddr_in address_of_server;
struct sockaddr_in address_of_client;
int socket_of_client;
int size_of_address_of_client = sizeof(address_of_client);
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0) {
printf("winsock initialization success\n");
}
else {
printf("winsock initialization failure\n");
}
SOCKET socket_of_server = socket(AF_INET, SOCK_STREAM, 0);
if (socket_of_server == -1) {
printf("creating socket failure\n");
}
else {
printf("creating socket success\n");
}
memset(&address_of_server, 0, sizeof(address_of_server));
address_of_server.sin_family = AF_INET;
//address_of_server.sin_family = PF_INET;
address_of_server.sin_addr.s_addr = htonl(INADDR_ANY);
address_of_server.sin_port = htons(8888);
bind(socket_of_server, (struct sockaddr*)&address_of_server, sizeof(address_of_server));
listen(socket_of_server, 5);
ClientSocket = accept(socket_of_server, NULL, NULL);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(socket_of_server);
WSACleanup();
return 1;
}
socket_of_client = accept(socket_of_server, (struct sockaddr*)&address_of_client, &size_of_address_of_client);
WSACleanup();
C# Client Code
public CCRMain()
{
InitializeComponent();
clientSocket.Connect("127.0.0.1", 8888);
}
void Data()
{
MainWindow mw = (MainWindow)Application.Current.MainWindow;
NetworkStream serverStream = mw.clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(A1G1.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
byte[] inStream = new byte[10025];
serverStream.Read(inStream, 0, (int)mw.clientSocket.ReceiveBufferSize);
System.IO.MemoryStream ms = new System.IO.MemoryStream(inStream);
System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
int[] inComingData = new int[5];
for (int i = 0; i < 5; i++)
{
inComingData[i] = br.ReadInt32();
Debug.WriteLine(inComingData[i].ToString());
A1G1Text.Text = inComingData[0].ToString();
}
}
NetworkStream.Read() is blocking. So as long as the server does not close the connection, this will not return until 10025 bytes have been read.
You should directly read from the network stream, not copy it over twice (or even four times in your case: From network to inStream and then to a MemoryStream and then to inCommingData and then to your text variable).
Use the NetworkStream.ReadTimeout property to indicate that you want Read() to return even if not the entire buffer was filed within some time.
I am working on socket C#. I've implemented a client server application using socket, but the problem is that the client doesn't receive all data sent by the server.
Here is the client application code. What should I do so that it would receive all data sent by the server?
strRecieved = "";
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9001);
soc.Connect(endPoint);
byte[] msgBuffer = Encoding.Default.GetBytes(path);
soc.Send(msgBuffer, 0, msgBuffer.Length, 0);
byte[] buffer = new byte[2000];
int rec = soc.Receive(buffer);
strRecieved = String.Format(Encoding.Default.GetString(buffer));
First of all. If you're implementing some kind of streaming feature ( tcp/udp/file ) you should consider using some kind of protocol.
What is a protocol? It's just a scheme to use when streaming data. Example:
[4Bytes - length][lengthBytes - message][1Byte - termination indicator]
Knowing the protocol you can read all of the incoming bytes simply as such :
byte[] buffer = new byte[4];
stream.ReadBytes(buffer, 0, 4); // cast that to int and read the rest
int packetLen = BitConverter.ToInt32(buffer, 0);
buffer = new byte[packetLen];
stream.ReadBytes(buffer, 0, buffer.Length); // all bytes that was sent
Remember that you have to subtract thease 4 bytes in the length before sending the message.
EDIT:
Simple example on how to send and receive data using shared protocol.
// sender.cs
string _stringToSend = "some fancy string";
byte[] encodedString = Encoding.UTF8.GetBytes(_stringToSend);
List<byte> buffer = new List<byte>();
buffer.AddRange(BitConverter.GetBytes(encodedString.Length));
buffer.AddRange(encodedString);
netStream.WriteBytes(buffer.ToArray(), 0, buffer.Count);
// netStream sent message in protocol [#LEN - 4Bytes][#MSG - #LENBytes]
// simply speaking something like: 5ABCDE
// receiver.cs
byte[] buffer = new byte[sizeof(int)];
netStream.ReadBytes(buffer, 0, buffer.Length);
// receiver got the length of the message eg. 5
int dataLen = BitConverter.ToInt32(buffer, 0);
buffer = new byte[dataLen];
// now we can read an actual message because we know it's length
netStream.ReadBytes(buffer, 0, buffer.Length);
string receivedString = Encoding.UTF8.GetString(buffer);
// received string is equal to "some fancy string"
Making it simpler
This technique forces you to use desired protocol which in this example will be :
First 4 bytes sizeof(int) are indicating the length of the incoming packet
Every byte further is your packet until the end.
So right now you should make ProtocolHelper object:
public static class ProtocolHelper
{
public byte[] PackIntoProtocol(string message)
{
List<byte> result = new List<byte>();
byte[] messageBuffer = Encoding.UTF8.GetBytes(message);
result.AddRange(BitConverter.GetBytes(messageBuffer.Length), 0); // this is the first part of the protocol ( length of the message )
result.AddRange(messageBuffer); // this is actual message
return result.ToArray();
}
public string UnpackProtocol(byte[] buffer)
{
return Encoding.UTF8.GetString(buffer, 0, buffer.Length);
}
}
Now ( depending on method you've chosen to read from network ) you have to send and receive your message.
// sender.cs
string meMessage = "network message 1";
byte[] buffer = ProtocolHelper.PackIntoProtocol(meMessage);
socket.Send(buffer, 0, buffer.Length, 0);
// receiver.cs
string message = string.Empty;
byte[] buffer = new byte[sizeof(int)]; // or simply new byte[4];
int received = socket.Receive(buffer);
if(received == sizeof(int))
{
int packetLen = BitConverter.ToInt32(buffer);// size of our message
buffer = new byte[packetLen];
received = socket.Receive(buffer);
if( packetLen == received ) // we have full buffer
{
message = PacketHelper.UnpackProtocol(buffer);
}
}
Console.WriteLine(message); // output: "network message 1"
You're limiting the size of received messages by 2KB as you're using new byte[2000].
I think you could either:
Size up you buffer to meet you message's size needs; and/or
Split you message into more than one socket messages.
Given that 4-8K is a good size for buffering socket messages and assuming RAM size is not a issue I would start with that, say, new byte[8000].
Also, you can send socket messages splitted in chunks. Maybe this is a good idea for the case. For example, if you have msg as the message (or object) you want to send:
private static async Task SendAnswer(Message msg, WebSocket socket)
{
var answer = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg).ToCharArray());
var bufferSize = 8000;
var endOfMessage = false;
for (var offset = 0; offset < answer.Length; offset += bufferSize)
{
if (offset + bufferSize >= answer.Length)
{
bufferSize = answer.Length - offset;
endOfMessage = true;
}
await socket.SendAsync(new ArraySegment<byte>(answer, offset, bufferSize),
WebSocketMessageType.Text, endOfMessage, CancellationToken.None);
}
}
And when receiving, you can also split the reception in chunks, so you can control you buffer (and therefore you memory consumption). After hanlding the whole message, you should wait for another message from the client to do more stuff. Source
private async Task ReceiveMessage(WebSocket webSocket)
{
var buffer = new byte[8000];
var result = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
while (!result.CloseStatus.HasValue)
{
string msg = Encoding.UTF8.GetString(new ArraySegment<byte>(buffer, 0, result.Count).ToArray());
while (!result.EndOfMessage)
{
result = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
msg += Encoding.UTF8.GetString(new ArraySegment<byte>(buffer, 0, result.Count).ToArray());
}
//At this point, `msg` has the whole message you sent, you can do whatever you want with it.
// [...]
//After you handle the message, wait for another message from the client
result = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
}
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}
I try to get a connection between an UWP-programm and a "normal" C# application.
UWP:
StreamSocket s = new StreamSocket();
await socket.ConnectAsync(new HostName("localhost"), "8003");
BinaryWriter bw = new BinaryWriter(socket.OutputStream.AsStreamForWrite());
String toSend = "Hello World!";
byte[] text = Encoding.UTF8.GetBytes(toSend);
byte[] number = BitConverter.getBytes(text.Length);
if(BitConverter.isLittleEndian) Array.Reverse(number);
bw.Write(number, 0, number.Length);
bw.Write(text, 0, text.Length);
"normal" C#, after the TcpListener etablished a new ClientThread:
//client was accepted by the server and is an instance of TcpClient
BinaryReader br = new BinaryReader(client.GetStream());
byte[] number = new byte[4];
br.Read(number, 0, number.Length);
if(BitConverter.isLittleEndian) Array.Reverse(number);
int size = BitConverter.ToInt32(number);
byte[] buffer = new byte[size];
br.Read(buffer, 0, buffer.Length);
String recieved = Encoding.UTF8.GetString(buffer);
Debug.WriteLine(recieved);
But the server doesn´t recieves anything. And the int size is 0. But if i run the UWP-code on a "normal" C# application, by replacing the s.OutputStream.AsStreamForWrite() with client.GetStream() and using a TcpClient instead of an StreamSocket the server recieves the text. What am I doing wrong?
Greets Marcel
I'm Trying to create a code to send a TCP message to a server.
When I use AutoIT Script Language with this code:
Example()
Func Example()
Local $ConnectedSocket, $szData
Local $szIPADDRESS = "10.200.0.104"
Local $nPORT = 1040
; Start The TCP Services
TCPStartup()
; Initialize a variable to represent a connection
$ConnectedSocket = -1
;Attempt to connect to SERVER at its IP and PORT 1040
$ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)
; If there is an error... show it
If #error Then
MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & #error)
Else
$szData="0x0021601FA10706052B0C00815ABE14281206072B0C00821D8148A007A0050303000800000DA20B0201013006020200D30500"
TCPSend($ConnectedSocket, $szData)
EndIf
EndFunc;==>Example
Works fine but I need to write the same code in C#. I try to do this:
private static byte[] MessageToByteArray(string message, Encoding encoding)
{
var byteCount = encoding.GetByteCount(message);
if (byteCount > byte.MaxValue)
throw new ArgumentException("Message size is greater than 255 bytes in the provided encoding");
var byteArray = new byte[byteCount + 1];
byteArray[0] = (byte)byteCount;
encoding.GetBytes(message, 0, message.Length, byteArray, 1);
return byteArray;
}
public static void Main(string[] args)
{
const string message = "0x0021601FA10706052B0C00815ABE14281206072B0C00821D8148A007A0050303000800000DA20B0201013006020200D30500";
var byteArray = MessageToByteArray(message, Encoding.ASCII);
Socket m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse("10.200.0.104");
System.Net.IPEndPoint remoteEP = new IPEndPoint(ipAdd, 1040);
m_socClient.Connect(remoteEP);
try
{
m_socClient.Send(byteArray);
}
catch (SocketException se)
{
Console.WriteLine(se.Message.ToString());
}
}
But this code doesn't work. The server shows when he receives the command. With C# code, the server shows that have connected but the command doesn't execute.
Are you sure you have to prefix the length?
byteArray[0] = (byte)byteCount;
encoding.GetBytes(message, 0, message.Length, byteArray, 1);
Often you have a null-terminated string:
encoding.GetBytes(message, 0, message.Length, byteArray, 0);
byteArray[byteArray.Length - 1] = (byte)'\0';
I am trying to iterate each byte received over UDP and append that byte to a string (sbCardNo). The problem is the iteration for each byte is in the while loop, meaning a infinate loop (if I understand this correctly). How can I adapt the below code to add each byte to a string?
private void DoWork()
{
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 8888);
UdpClient newsock = new UdpClient(ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
StringBuilder sbCardNo = new StringBuilder();
while (true)
{
data = newsock.Receive(ref sender);
sbCardNo.Append(data.GetValue(0));
FileStream fs = new FileStream(folderPath + "\\AuthService.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamwriter = new StreamWriter(fs);
m_streamwriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamwriter.WriteLine(sbCardNo);
m_streamwriter.Flush();
m_streamwriter.Close();
}
}
The above code gives me the following -
2
252
25267
2526748
252674848
25267484870
2526748487069
25267484870693
I just need the last line - 25267484870693
Thanks
Create the file at the end of the while and write all the contents of the StringBuilder at once, and handle logic for possible excpetions
In this example its reading until the byte 'Z' is received, but is much better if the other side of this comunication sends a single String or that the first 2 bytes is the length of the bytes that will be sent
private void DoWork()
{
byte ch ;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 8888);
UdpClient newsock = new UdpClient(ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
StringBuilder sbCardNo = new StringBuilder();
while (true)
{
do
{
data = newsock.Receive(ref sender);
ch = data.GetValue(0)
sbCardNo.Append(ch);
} while (ch != 'Z') ;
using (StreamWriter m_streamwriter = new StreamWriter( folderPath + "\\AuthService.txt"))
{
m_streamwriter.WriteLine(sbCardNo.toString());
}
}
}