467,219 Members | 1,439 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,219 developers. It's quick & easy.

multithread c# socket server

Hello:

I write a multithread c# socket server,it is a winform application,there is
a richtextbox control and button,when the button is click,the server begin
to listen the socket port,waiting for a incoming connection,the relative
code snipprt as following::

private IPAddress myIP=IPAddress.Parse("127.0.0.1");

private IPEndPoint myServer;

private Socket socket;

private Socket accSocket;

private System.Windows.Forms.Button button2;

private bool check;

private void button1_Click(object sender, System.EventArgs e)

{

check=true;

try

{

Thread thread =new Thread(new ThreadStart(accp));

thread.Start();

}

catch(Exception ee)

{

MessageBox.Show(ee.Message);

}

}

private void accp()

{

myServer=new IPEndPoint(myIP,Int32.Parse("20000"));

socket=new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

socket.Bind(myServer);

socket.Listen(50);

while(true)

{

try

{

accSocket=socket.Accept();

if(accSocket.Connected)

{

Thread thread=new Thread(new ThreadStart(round));

thread.Start();

}

}

catch(Exception ee)

{

MessageBox.Show(ee.Message);

}

}

}

private void round()

{

Byte[] rec=new Byte[1024];

NetworkStream acceptStream=new NetworkStream(accSocket);

int i=0;

while((i=acceptStream.Read(rec,0,rec.Length))!=0)

{

string
recMessage=System.Text.Encoding.Default.GetString( rec);

rec=new Byte[1024];

this.richTextBox1.AppendText(recMessage);

}

}

***************.

***************



In order to test the server,I write a multithread client too,there is only
one button in the form,when it is clicked,four threads is generated to
connect the server simultaneously,each thread write one line to the server
socket.code as:



private IPAddress myIP;

private IPEndPoint myServer;

private Socket socket;

private void button1_Click(object sender, System.EventArgs e)

{

for(int i=0;i<4;i++)

{

Thread thread=new Thread(new ThreadStart(round));

thread.Start();

thread.Join();

}

}

private void round()

{

try

{

myIP=IPAddress.Parse("127.0.0.1");

myServer=new IPEndPoint(myIP,Int32.Parse("20000"));

socket=new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

socket.Connect(myServer);

NetworkStream netStream=new NetworkStream(socket);

Byte[] byteMessage=new Byte[640];

string sendMessage="Һã!\r\n";
byteMessage=System.Text.Encoding.Default.GetBytes( sendMessage.ToCharArray())
;

// socket.Send(byteMessage,byteMessage.Length,0);

netStream.Write(byteMessage,0,byteMessage.Length);

netStream.Flush();

netStream.Close();

socket.Close();

}

catch(Exception ee)

{

MessageBox.Show(ee.Message);

}

}



As you can see,when I click the button in the client side,I should see four
lines is printed in the server side.but in fact,I cant,I can only see one
line,sometimes two or three lines,by my tracing,I found they always come
from the last several threads.if I modify the button click event in the
client as following(add sleep between the threads),it works well,that is I
can see four lines every time in the server:

private void button1_Click(object sender, System.EventArgs e)

{

for(int i=0;i<4;i++)

{

Thread thread=new Thread(new ThreadStart(round));

thread.Start();

thread.Join();

Thread.Sleep(100);

}

}

If I modify the timeout param of the sleep method to lower and lower, the
mentioned problem occur again.i also write a java multithread server, it
works well to the simuteneous thread connection.

Why c# socket server cant handle the simultaneous access?is it a bug?by
comparing the java server and c# server,I also found that the java server is
faster then c# server.

Any instruction?Thank you.


Nov 16 '05 #1
  • viewed: 25775
Share:
2 Replies
You do not have to repost you message over and over. As far as I see 6
messages from now you recieved an answer from community member, Henrik Dahl,
to your question. So why repost? Use the current thread to continue
conversation if needed...

Sorry for this,

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"zhebincong" <co********@capinfo.com.cn> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hello:

I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin
to listen the socket port,waiting for a incoming connection,the relative
code snipprt as following::

private IPAddress myIP=IPAddress.Parse("127.0.0.1");

private IPEndPoint myServer;

private Socket socket;

private Socket accSocket;

private System.Windows.Forms.Button button2;

private bool check;

private void button1_Click(object sender, System.EventArgs e)

{

check=true;

try

{

Thread thread =new Thread(new ThreadStart(accp));

thread.Start();

}

catch(Exception ee)

{

MessageBox.Show(ee.Message);

}

}

private void accp()

{

myServer=new IPEndPoint(myIP,Int32.Parse("20000"));

socket=new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

socket.Bind(myServer);

socket.Listen(50);

while(true)

{

try

{

accSocket=socket.Accept();

if(accSocket.Connected)

{

Thread thread=new Thread(new ThreadStart(round));

thread.Start();

}

}

catch(Exception ee)

{

MessageBox.Show(ee.Message);

}

}

}

private void round()

{

Byte[] rec=new Byte[1024];

NetworkStream acceptStream=new NetworkStream(accSocket);

int i=0;

while((i=acceptStream.Read(rec,0,rec.Length))!=0)

{

string
recMessage=System.Text.Encoding.Default.GetString( rec);

rec=new Byte[1024];

this.richTextBox1.AppendText(recMessage);

}

}

***************.

***************



In order to test the server,I write a multithread client too,there is only
one button in the form,when it is clicked,four threads is generated to
connect the server simultaneously,each thread write one line to the server
socket.code as:



private IPAddress myIP;

private IPEndPoint myServer;

private Socket socket;

private void button1_Click(object sender, System.EventArgs e)

{

for(int i=0;i<4;i++)

{

Thread thread=new Thread(new ThreadStart(round));

thread.Start();

thread.Join();

}

}

private void round()

{

try

{

myIP=IPAddress.Parse("127.0.0.1");

myServer=new IPEndPoint(myIP,Int32.Parse("20000"));

socket=new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

socket.Connect(myServer);

NetworkStream netStream=new NetworkStream(socket);

Byte[] byteMessage=new Byte[640];

string sendMessage="Һã!\r\n";
byteMessage=System.Text.Encoding.Default.GetBytes( sendMessage.ToCharArray()) ;

// socket.Send(byteMessage,byteMessage.Length,0);

netStream.Write(byteMessage,0,byteMessage.Length);

netStream.Flush();

netStream.Close();

socket.Close();

}

catch(Exception ee)

{

MessageBox.Show(ee.Message);

}

}



As you can see,when I click the button in the client side,I should see four lines is printed in the server side.but in fact,I cant,I can only see one line,sometimes two or three lines,by my tracing,I found they always come
from the last several threads.if I modify the button click event in the
client as following(add sleep between the threads),it works well,that is I
can see four lines every time in the server:

private void button1_Click(object sender, System.EventArgs e)

{

for(int i=0;i<4;i++)

{

Thread thread=new Thread(new ThreadStart(round));

thread.Start();

thread.Join();

Thread.Sleep(100);

}

}

If I modify the timeout param of the sleep method to lower and lower, the
mentioned problem occur again.i also write a java multithread server, it
works well to the simuteneous thread connection.

Why c# socket server cant handle the simultaneous access?is it a bug?by
comparing the java server and c# server,I also found that the java server is faster then c# server.

Any instruction?Thank you.

Nov 16 '05 #2
sorry,the other questions is posted from other forums,maybe microsoft community and several others ,and i only post one time in every forum.i don't know why they come here,maybe you are the same forum.



"Tamir Khason" wrote:
You do not have to repost you message over and over. As far as I see 6
messages from now you recieved an answer from community member, Henrik Dahl,
to your question. So why repost? Use the current thread to continue
conversation if needed...

Sorry for this,

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"zhebincong" <co********@capinfo.com.cn> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hello:

I write a multithread c# socket server,it is a winform application,there

is
a richtextbox control and button,when the button is click,the server begin
to listen the socket port,waiting for a incoming connection,the relative
code snipprt as following::

private IPAddress myIP=IPAddress.Parse("127.0.0.1");

private IPEndPoint myServer;

private Socket socket;

private Socket accSocket;

private System.Windows.Forms.Button button2;

private bool check;

private void button1_Click(object sender, System.EventArgs e)

{

check=true;

try

{

Thread thread =new Thread(new ThreadStart(accp));

thread.Start();

}

catch(Exception ee)

{

MessageBox.Show(ee.Message);

}

}

private void accp()

{

myServer=new IPEndPoint(myIP,Int32.Parse("20000"));

socket=new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

socket.Bind(myServer);

socket.Listen(50);

while(true)

{

try

{

accSocket=socket.Accept();

if(accSocket.Connected)

{

Thread thread=new Thread(new ThreadStart(round));

thread.Start();

}

}

catch(Exception ee)

{

MessageBox.Show(ee.Message);

}

}

}

private void round()

{

Byte[] rec=new Byte[1024];

NetworkStream acceptStream=new NetworkStream(accSocket);

int i=0;

while((i=acceptStream.Read(rec,0,rec.Length))!=0)

{

string
recMessage=System.Text.Encoding.Default.GetString( rec);

rec=new Byte[1024];

this.richTextBox1.AppendText(recMessage);

}

}

¡*¡*¡*¡*¡*¡*¡*¡*¡*¡*¡*¡*¡ *¡*¡*.

¡*¡*¡*¡*¡*¡*¡*¡*¡*¡*¡*¡*¡ *¡*¡*



In order to test the server,I write a multithread client too,there is only
one button in the form,when it is clicked,four threads is generated to
connect the server simultaneously,each thread write one line to the server
socket.code as:



private IPAddress myIP;

private IPEndPoint myServer;

private Socket socket;

private void button1_Click(object sender, System.EventArgs e)

{

for(int i=0;i<4;i++)

{

Thread thread=new Thread(new ThreadStart(round));

thread.Start();

thread.Join();

}

}

private void round()

{

try

{

myIP=IPAddress.Parse("127.0.0.1");

myServer=new IPEndPoint(myIP,Int32.Parse("20000"));

socket=new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

socket.Connect(myServer);

NetworkStream netStream=new NetworkStream(socket);

Byte[] byteMessage=new Byte[640];

string sendMessage="´ó¼ÒºÃ£¡£¡£¡£¡!\r\n";

byteMessage=System.Text.Encoding.Default.GetBytes( sendMessage.ToCharArray())
;

// socket.Send(byteMessage,byteMessage.Length,0);

netStream.Write(byteMessage,0,byteMessage.Length);

netStream.Flush();

netStream.Close();

socket.Close();

}

catch(Exception ee)

{

MessageBox.Show(ee.Message);

}

}



As you can see,when I click the button in the client side,I should see

four
lines is printed in the server side.but in fact,I can¡¯t,I can only see

one
line,sometimes two or three lines,by my tracing,I found they always come
from the last several threads.if I modify the button click event in the
client as following(add sleep between the threads),it works well,that is I
can see four lines every time in the server:

private void button1_Click(object sender, System.EventArgs e)

{

for(int i=0;i<4;i++)

{

Thread thread=new Thread(new ThreadStart(round));

thread.Start();

thread.Join();

Thread.Sleep(100);

}

}

If I modify the timeout param of the sleep method to lower and lower, the
mentioned problem occur again.i also write a java multithread server, it
works well to the simuteneous thread connection.

Why c# socket server can¡¯t handle the simultaneous access?is it a bug?by
comparing the java server and c# server,I also found that the java server

is
faster then c# server.

Any instruction?Thank you.


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by simon place | last post: by
reply views Thread by r_obert@REMOVE_THIS.hotmail.com | last post: by
2 posts views Thread by Rene Srensen | last post: by
4 posts views Thread by Crirus | last post: by
4 posts views Thread by Engineerik | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.