C
C#13mo ago
Ronnie

send a response from TCP server to client

Exception says that objektreference is null Initiates these directly in Form
c#
TcpListener _lyssnare;
TcpClient _klient;
int _port = 12345;
IPAddress _ipAddress = IPAddress.Parse("127.0.0.1");
bool _ärServerIgång;
Kodhanterare _kodhanterare = new Kodhanterare();
c#
TcpListener _lyssnare;
TcpClient _klient;
int _port = 12345;
IPAddress _ipAddress = IPAddress.Parse("127.0.0.1");
bool _ärServerIgång;
Kodhanterare _kodhanterare = new Kodhanterare();
Starts server
c#
public async void StartaServer()
{
try
{
_lyssnare = new TcpListener(_ipAddress, _port);
_lyssnare.Start();
_ärServerIgång = true;


/// <summary>
/// Väntar på meddelande
/// </summary>
try
{
lbl_info.Text = "väntar på koppling...";
_klient = await _lyssnare.AcceptTcpClientAsync();
}
catch (ObjectDisposedException ex)
{
return;
}
lbl_info.Text = "kopplad till klient";
StartaLäsning(_klient);
}
catch (Exception error)
{
MessageBox.Show(error.Message); return;
}
}
c#
public async void StartaServer()
{
try
{
_lyssnare = new TcpListener(_ipAddress, _port);
_lyssnare.Start();
_ärServerIgång = true;


/// <summary>
/// Väntar på meddelande
/// </summary>
try
{
lbl_info.Text = "väntar på koppling...";
_klient = await _lyssnare.AcceptTcpClientAsync();
}
catch (ObjectDisposedException ex)
{
return;
}
lbl_info.Text = "kopplad till klient";
StartaLäsning(_klient);
}
catch (Exception error)
{
MessageBox.Show(error.Message); return;
}
}
Sends back response
c#
public async void StartaSändning(string meddelande)
{
// _klient in here is NULL despite initiated in Form and "used" in StartServer()

byte[] utdata = Encoding.Unicode.GetBytes(meddelande);
try
{
await _klient.GetStream().WriteAsync(utdata, 0, utdata.Length);
}
catch (Exception error)
{ MessageBox.Show(error.Message); return; }
}
c#
public async void StartaSändning(string meddelande)
{
// _klient in here is NULL despite initiated in Form and "used" in StartServer()

byte[] utdata = Encoding.Unicode.GetBytes(meddelande);
try
{
await _klient.GetStream().WriteAsync(utdata, 0, utdata.Length);
}
catch (Exception error)
{ MessageBox.Show(error.Message); return; }
}
12 Replies
Buddy
Buddy13mo ago
Exception says that objektreference is null
Which line does it break at? Send a snippet of said line. $code
MODiX
MODiX13mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Ronnie
Ronnie13mo ago
It breaks at
await _klient.GetStream().WriteAsync(utdata, 0, utdata.Length);
await _klient.GetStream().WriteAsync(utdata, 0, utdata.Length);
In the method that sends back response
Buddy
Buddy13mo ago
Then I bet _klient is null
Ronnie
Ronnie13mo ago
It is, but I dont understand why - does it not uphold a connection after intially connecting in the StartServer method?
try
{
lbl_info.Text = "väntar på koppling...";
_klient = await _lyssnare.AcceptTcpClientAsync();
}
try
{
lbl_info.Text = "väntar på koppling...";
_klient = await _lyssnare.AcceptTcpClientAsync();
}
Buddy
Buddy13mo ago
please format the code correctly.
MODiX
MODiX13mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Ronnie
Ronnie13mo ago
_klient = await _lyssnare.AcceptTcpClientAsync();
_klient = await _lyssnare.AcceptTcpClientAsync();
When I paste from VS, it jumbles it up sorry
Buddy
Buddy13mo ago
Please use the site mentioned then paste the entire code.
Ronnie
Ronnie13mo ago
BlazeBin - pmehvdocimdv
A tool for sharing your source code with the world!
MODiX
MODiX13mo ago
Please don't upload any potentially harmful files @ronnieyw, your message has been removed
Ronnie
Ronnie13mo ago
Done, It works with a klient but i couldnt post it here beacuse .exe file i think