Dear fellow programmers! :D
I'm actually having an issue with sending strings between a client and a server.
The string i send from the client does arrive and is used by the server correctly.
But i wrote a method for the server to confirm that a string arrived.
(I am doing this for learning purposes)
Now, my problems are
1. The Client gets the answer only if i send the request multiple times
(*click* - no answer, but the server receives the string. *second click* - the server receives the string again and the client gets an answer)
2. If the Server sends a longer string (like the output of "ipconfig -all", it says the buffer size is too big.
I wrote a "checker" method that splits strings that are bigger than 100 chars, puts them in an array and then (via foreach)
sends every string in the array one by one.
Pastebin for the Server codePastebin for the Client codeI think the problems are inside the "BroadCastSend" and "Receive" methods, and maye the method that splits the string and calls the foreach.
this is my splitting/checking method
public void checker()
{
if (result.Length > 100)
{
string[] parray = SplitInParts(result, 100).ToArray();
foreach (string s in parray)
{
result = s;
ResultSend();
}
result = "";
return;
}
ResultSend();
}
and these are my Receive and Broadcast methods:
(SERVER)
public void ResultSend()
{
try
{
sendresult = result;
string TextBoxer = sendresult; //TEXT
string IPBoxer = opip; //EMPFÄNGER
byte[] myWriteBuffer = Encoding.ASCII.GetBytes(TextBoxer);
//Socket definieren
Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//EndPoint definieren bzw. Ziel des Broadcastes
IPEndPoint iep1 = new IPEndPoint(IPAddress.Parse(IPBoxer), 24711);
//Optionen auf den Socket binden
bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
//Broadcast senden
bcSocket.SendTo(myWriteBuffer, iep1);
//Socket schliessen, nach erfolgreichem Senden des Broadcastes
bcSocket.Close();
}
catch (Exception ex)
{
Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("Error sending answer, i guess the IP is invalid")));
return;
}
}
(CLIENT)
public void Receive()
{
try
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 24711);
sock.Bind(iep);
EndPoint ep = (EndPoint)iep;
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
data = new byte[1024];
recv = sock.ReceiveFrom(data, ref ep);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add(stringData)));
sock.Close();
sock.Dispose();
Receive();
// return;
}
catch (Exception ex)
{
MessageBox.Show("Fehler in Methode" + Environment.NewLine + Environment.NewLine + ex + Environment.NewLine + Environment.NewLine + "Möglicherweise ist ein Port blockiert.", "Ausführungsfehler!");
}
}
I was hoping someone can help me with this, i'm trying to solve this problem for a week and really tried my best.
Thank you !!