473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 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 #1
2 26462
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 #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 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...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.