C
C#2y 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
Buddy2y ago
Exception says that objektreference is null
Which line does it break at? Send a snippet of said line. $code
MODiX
MODiX2y 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
RonnieOP2y 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
Buddy2y ago
Then I bet _klient is null
Ronnie
RonnieOP2y 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
Buddy2y ago
please format the code correctly.
MODiX
MODiX2y 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
RonnieOP2y ago
_klient = await _lyssnare.AcceptTcpClientAsync();
_klient = await _lyssnare.AcceptTcpClientAsync();
When I paste from VS, it jumbles it up sorry
Buddy
Buddy2y ago
Please use the site mentioned then paste the entire code.
Ronnie
RonnieOP2y ago
BlazeBin - pmehvdocimdv
A tool for sharing your source code with the world!
MODiX
MODiX2y ago
Please don't upload any potentially harmful files @ronnieyw, your message has been removed
Ronnie
RonnieOP2y ago
Done, It works with a klient but i couldnt post it here beacuse .exe file i think
Want results from more Discord servers?
Add your server