Z
zhebincong
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.Stream,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.Stream,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.
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.Stream,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.Stream,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.