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. 2 26379
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: simon place |
last post by:
Spent some very frustrating hours recoding to find a way of closing a server
socket, i'd not thought it would be any problem,
however, after complete failure and as a last resort, i looked at the...
|
by: r_obert |
last post by:
Hello,
I'm trying to create a worker thread for my VC++ program, and was
wondering whether I should be linking with the Multithread /MT or
Multithread DLL /MD option? I'm not quite sure, in...
|
by: zbcong |
last post by:
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...
|
by: Chris Tanger |
last post by:
Context:
C#
System.Net.Sockets
Socket created with constructor prarmeters Internetwork, Stream and TCP
everything else is left at the default parameters and options except linger
may be changed...
|
by: Rene Sørensen |
last post by:
We are 4 students working on a assignment, that our teacher gave use,
normally we do this is C++, but the 4 of us, use C# more often that
C++ so…
We made a small games called reversi, now our job...
|
by: Crirus |
last post by:
hello
I need a sample of a server with multithreading accept connections with
sockets...
Have anyone?
Thanks,
Crirus
|
by: Engineerik |
last post by:
I am trying to create a socket server which will listen for connections from
multiple clients and call subroutines in a Fortran DLL and pass the results
back to the client. The asynchronous socket...
|
by: keksy |
last post by:
Hi every1,
I am writing a small client/server application and in it I want to
send an image asynchronous from the client to the server through a TCP
socket. I found an example code on the MSDN...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
| |