472,792 Members | 4,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,792 software developers and data experts.

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
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 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
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...
0
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...
4
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...
4
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...
2
by: Rene Srensen | 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...
4
by: Crirus | last post by:
hello I need a sample of a server with multithreading accept connections with sockets... Have anyone? Thanks, Crirus
4
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...
1
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...
0
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...
0
linyimin
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...
0
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...
0
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...
14
DJRhino1175
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...
0
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...
5
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...
0
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=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.