ClientSocket is null after connecting socket clientPrevent flashing whilst typing/copying to rich text boxC# Opening a Form, then closing it from another methodhow to kill and create the new image at same port using threading in C#Receiving irrelevant data from network on second call to network using tcp socketsOther computers can't connect my programFilename pattern not working in OpenFileDialogLoosing one Byte over TCP SocketSocket throws nullreferenceexception while asynchrously receiving datac# AsyncSocket Server need locking?The I / O was aborted because of a thread end or an application request
How does the math work for Perception checks?
Open a doc from terminal, but not by its name
Calculating total slots
Did arcade monitors have same pixel aspect ratio as TV sets?
Using substitution ciphers to generate new alphabets in a novel
Why is it that I can sometimes guess the next note?
It grows, but water kills it
Mimic lecturing on blackboard, facing audience
A social experiment. What is the worst that can happen?
Is there a way to get `mathscr' with lower case letters in pdfLaTeX?
Lowest total scrabble score
Store Credit Card Information in Password Manager?
Why should universal income be universal?
Has any country ever had 2 former presidents in jail simultaneously?
Can I still be respawned if I die by falling off the map?
Biological Blimps: Propulsion
PTIJ: Haman's bad computer
Why Shazam when there is already Superman?
What if a revenant (monster) gains fire resistance?
Keeping a ball lost forever
Picking the different solutions to the time independent Schrodinger eqaution
Why "had" in "[something] we would have made had we used [something]"?
Are Captain Marvel's powers affected by Thanos' actions in Infinity War
How to cover method return statement in Apex Class?
ClientSocket is null after connecting socket client
Prevent flashing whilst typing/copying to rich text boxC# Opening a Form, then closing it from another methodhow to kill and create the new image at same port using threading in C#Receiving irrelevant data from network on second call to network using tcp socketsOther computers can't connect my programFilename pattern not working in OpenFileDialogLoosing one Byte over TCP SocketSocket throws nullreferenceexception while asynchrously receiving datac# AsyncSocket Server need locking?The I / O was aborted because of a thread end or an application request
I use windows7 and VS2010 to write a C# winform program.
In the program I run start a TCP socket server and a socket client. After the client socket is accepted, I send command to the client.
When I send data, error occurs: System.NullReferenceException.
at this line:
myClientSocket.Send(bs3, bs3.Length, 0);
I have debug the program and the socket server and client could be created successfully. The client could also be connect to the server successfully.I think maybe myclientsocket is none so the command could not be sent. But I do not know why.
The code:
public partial class Form1 : Form
public static Socket myClientSocket = null;
protected override void OnLoad(EventArgs e)
SocketServie();
Thread.Sleep(1000);
runclient();
Thread.Sleep(2000);
read();
public void SocketServie()
string host = "127.0.0.1";
int port = 8024;
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true); //port reuse
socket.Bind(new IPEndPoint(IPAddress.Parse(host), port));
socket.Listen(100);
Thread SocketThread = new Thread(this.ListenClientConnect);
SocketThread.IsBackground = true;
SocketThread.Start();
private void ListenClientConnect()
while (true)
this.clientSocket = socket.Accept();
Thread receiveThread = new Thread(new
ParameterizedThreadStart(ReceiveMessage));
receiveThread.IsBackground = true;
receiveThread.Start(this);
Thread.Sleep(1000);
public void ReceiveMessage(object Form1)
Form2 f5 = new Form2();
//Socket myClientSocket = (Socket)clientSocket;
myClientSocket = (Socket)clientSocket;
string InitCurMeter = "INIT_CUR_METERrn";
byte[] bs = Encoding.UTF8.GetBytes(InitCurMeter);
myClientSocket.Send(bs, bs.Length, 0);
Thread.Sleep(2000);
string InitVolMeter = "INIT_VOL_METERrn";
byte[] bs2 =
Encoding.UTF8.GetBytes(InitVolMeter);//Encoding.UTF8.GetBytes()
myClientSocket.Send(bs2, bs2.Length, 0);
public void runclient(object sender, EventArgs e)
try
using (Process myProcess = new Process())
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "socket_client.exe";
myProcess.StartInfo.Arguments = "-p 8024";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
catch (Exception error)
public static void read()
try
string readCurMeter = "READrn";
byte[] bs3 = Encoding.UTF8.GetBytes(readCurMeter);
myClientSocket.Send(bs3, bs3.Length, 0);
catch (Exception e5)
MessageBox.Show("read_cur error:" + e5.Message);
c#
add a comment |
I use windows7 and VS2010 to write a C# winform program.
In the program I run start a TCP socket server and a socket client. After the client socket is accepted, I send command to the client.
When I send data, error occurs: System.NullReferenceException.
at this line:
myClientSocket.Send(bs3, bs3.Length, 0);
I have debug the program and the socket server and client could be created successfully. The client could also be connect to the server successfully.I think maybe myclientsocket is none so the command could not be sent. But I do not know why.
The code:
public partial class Form1 : Form
public static Socket myClientSocket = null;
protected override void OnLoad(EventArgs e)
SocketServie();
Thread.Sleep(1000);
runclient();
Thread.Sleep(2000);
read();
public void SocketServie()
string host = "127.0.0.1";
int port = 8024;
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true); //port reuse
socket.Bind(new IPEndPoint(IPAddress.Parse(host), port));
socket.Listen(100);
Thread SocketThread = new Thread(this.ListenClientConnect);
SocketThread.IsBackground = true;
SocketThread.Start();
private void ListenClientConnect()
while (true)
this.clientSocket = socket.Accept();
Thread receiveThread = new Thread(new
ParameterizedThreadStart(ReceiveMessage));
receiveThread.IsBackground = true;
receiveThread.Start(this);
Thread.Sleep(1000);
public void ReceiveMessage(object Form1)
Form2 f5 = new Form2();
//Socket myClientSocket = (Socket)clientSocket;
myClientSocket = (Socket)clientSocket;
string InitCurMeter = "INIT_CUR_METERrn";
byte[] bs = Encoding.UTF8.GetBytes(InitCurMeter);
myClientSocket.Send(bs, bs.Length, 0);
Thread.Sleep(2000);
string InitVolMeter = "INIT_VOL_METERrn";
byte[] bs2 =
Encoding.UTF8.GetBytes(InitVolMeter);//Encoding.UTF8.GetBytes()
myClientSocket.Send(bs2, bs2.Length, 0);
public void runclient(object sender, EventArgs e)
try
using (Process myProcess = new Process())
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "socket_client.exe";
myProcess.StartInfo.Arguments = "-p 8024";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
catch (Exception error)
public static void read()
try
string readCurMeter = "READrn";
byte[] bs3 = Encoding.UTF8.GetBytes(readCurMeter);
myClientSocket.Send(bs3, bs3.Length, 0);
catch (Exception e5)
MessageBox.Show("read_cur error:" + e5.Message);
c#
My first suggestion would be to separate concerns. Why are forms involved in socket work? Secondly, we have mystery global variables likemyClientSocket
and the even more mysterioussocket
that I assume is also a global somewhere. Most servers should be written to expect multiple connections (even if that's just a reconnect after a previous disconnect from a single client) and you should wrap up that "connection" concept in a class that owns the relevant socket, tracks state, buffers, etc.
– Damien_The_Unbeliever
Mar 7 at 7:21
add a comment |
I use windows7 and VS2010 to write a C# winform program.
In the program I run start a TCP socket server and a socket client. After the client socket is accepted, I send command to the client.
When I send data, error occurs: System.NullReferenceException.
at this line:
myClientSocket.Send(bs3, bs3.Length, 0);
I have debug the program and the socket server and client could be created successfully. The client could also be connect to the server successfully.I think maybe myclientsocket is none so the command could not be sent. But I do not know why.
The code:
public partial class Form1 : Form
public static Socket myClientSocket = null;
protected override void OnLoad(EventArgs e)
SocketServie();
Thread.Sleep(1000);
runclient();
Thread.Sleep(2000);
read();
public void SocketServie()
string host = "127.0.0.1";
int port = 8024;
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true); //port reuse
socket.Bind(new IPEndPoint(IPAddress.Parse(host), port));
socket.Listen(100);
Thread SocketThread = new Thread(this.ListenClientConnect);
SocketThread.IsBackground = true;
SocketThread.Start();
private void ListenClientConnect()
while (true)
this.clientSocket = socket.Accept();
Thread receiveThread = new Thread(new
ParameterizedThreadStart(ReceiveMessage));
receiveThread.IsBackground = true;
receiveThread.Start(this);
Thread.Sleep(1000);
public void ReceiveMessage(object Form1)
Form2 f5 = new Form2();
//Socket myClientSocket = (Socket)clientSocket;
myClientSocket = (Socket)clientSocket;
string InitCurMeter = "INIT_CUR_METERrn";
byte[] bs = Encoding.UTF8.GetBytes(InitCurMeter);
myClientSocket.Send(bs, bs.Length, 0);
Thread.Sleep(2000);
string InitVolMeter = "INIT_VOL_METERrn";
byte[] bs2 =
Encoding.UTF8.GetBytes(InitVolMeter);//Encoding.UTF8.GetBytes()
myClientSocket.Send(bs2, bs2.Length, 0);
public void runclient(object sender, EventArgs e)
try
using (Process myProcess = new Process())
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "socket_client.exe";
myProcess.StartInfo.Arguments = "-p 8024";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
catch (Exception error)
public static void read()
try
string readCurMeter = "READrn";
byte[] bs3 = Encoding.UTF8.GetBytes(readCurMeter);
myClientSocket.Send(bs3, bs3.Length, 0);
catch (Exception e5)
MessageBox.Show("read_cur error:" + e5.Message);
c#
I use windows7 and VS2010 to write a C# winform program.
In the program I run start a TCP socket server and a socket client. After the client socket is accepted, I send command to the client.
When I send data, error occurs: System.NullReferenceException.
at this line:
myClientSocket.Send(bs3, bs3.Length, 0);
I have debug the program and the socket server and client could be created successfully. The client could also be connect to the server successfully.I think maybe myclientsocket is none so the command could not be sent. But I do not know why.
The code:
public partial class Form1 : Form
public static Socket myClientSocket = null;
protected override void OnLoad(EventArgs e)
SocketServie();
Thread.Sleep(1000);
runclient();
Thread.Sleep(2000);
read();
public void SocketServie()
string host = "127.0.0.1";
int port = 8024;
socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true); //port reuse
socket.Bind(new IPEndPoint(IPAddress.Parse(host), port));
socket.Listen(100);
Thread SocketThread = new Thread(this.ListenClientConnect);
SocketThread.IsBackground = true;
SocketThread.Start();
private void ListenClientConnect()
while (true)
this.clientSocket = socket.Accept();
Thread receiveThread = new Thread(new
ParameterizedThreadStart(ReceiveMessage));
receiveThread.IsBackground = true;
receiveThread.Start(this);
Thread.Sleep(1000);
public void ReceiveMessage(object Form1)
Form2 f5 = new Form2();
//Socket myClientSocket = (Socket)clientSocket;
myClientSocket = (Socket)clientSocket;
string InitCurMeter = "INIT_CUR_METERrn";
byte[] bs = Encoding.UTF8.GetBytes(InitCurMeter);
myClientSocket.Send(bs, bs.Length, 0);
Thread.Sleep(2000);
string InitVolMeter = "INIT_VOL_METERrn";
byte[] bs2 =
Encoding.UTF8.GetBytes(InitVolMeter);//Encoding.UTF8.GetBytes()
myClientSocket.Send(bs2, bs2.Length, 0);
public void runclient(object sender, EventArgs e)
try
using (Process myProcess = new Process())
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "socket_client.exe";
myProcess.StartInfo.Arguments = "-p 8024";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
catch (Exception error)
public static void read()
try
string readCurMeter = "READrn";
byte[] bs3 = Encoding.UTF8.GetBytes(readCurMeter);
myClientSocket.Send(bs3, bs3.Length, 0);
catch (Exception e5)
MessageBox.Show("read_cur error:" + e5.Message);
c#
c#
edited Mar 7 at 7:05
Everyone
910319
910319
asked Mar 7 at 6:35
marrymarry
23
23
My first suggestion would be to separate concerns. Why are forms involved in socket work? Secondly, we have mystery global variables likemyClientSocket
and the even more mysterioussocket
that I assume is also a global somewhere. Most servers should be written to expect multiple connections (even if that's just a reconnect after a previous disconnect from a single client) and you should wrap up that "connection" concept in a class that owns the relevant socket, tracks state, buffers, etc.
– Damien_The_Unbeliever
Mar 7 at 7:21
add a comment |
My first suggestion would be to separate concerns. Why are forms involved in socket work? Secondly, we have mystery global variables likemyClientSocket
and the even more mysterioussocket
that I assume is also a global somewhere. Most servers should be written to expect multiple connections (even if that's just a reconnect after a previous disconnect from a single client) and you should wrap up that "connection" concept in a class that owns the relevant socket, tracks state, buffers, etc.
– Damien_The_Unbeliever
Mar 7 at 7:21
My first suggestion would be to separate concerns. Why are forms involved in socket work? Secondly, we have mystery global variables like
myClientSocket
and the even more mysterious socket
that I assume is also a global somewhere. Most servers should be written to expect multiple connections (even if that's just a reconnect after a previous disconnect from a single client) and you should wrap up that "connection" concept in a class that owns the relevant socket, tracks state, buffers, etc.– Damien_The_Unbeliever
Mar 7 at 7:21
My first suggestion would be to separate concerns. Why are forms involved in socket work? Secondly, we have mystery global variables like
myClientSocket
and the even more mysterious socket
that I assume is also a global somewhere. Most servers should be written to expect multiple connections (even if that's just a reconnect after a previous disconnect from a single client) and you should wrap up that "connection" concept in a class that owns the relevant socket, tracks state, buffers, etc.– Damien_The_Unbeliever
Mar 7 at 7:21
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037481%2fclientsocket-is-null-after-connecting-socket-client%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037481%2fclientsocket-is-null-after-connecting-socket-client%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
My first suggestion would be to separate concerns. Why are forms involved in socket work? Secondly, we have mystery global variables like
myClientSocket
and the even more mysterioussocket
that I assume is also a global somewhere. Most servers should be written to expect multiple connections (even if that's just a reconnect after a previous disconnect from a single client) and you should wrap up that "connection" concept in a class that owns the relevant socket, tracks state, buffers, etc.– Damien_The_Unbeliever
Mar 7 at 7:21