473,326 Members | 2,173 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,326 software developers and data experts.

Sockets and threads

Hello I am new to threads and I don't have too much idea. So as to
learn I was thinking to create a simple multithreaded server. There
are several clients that send data to the server as a kind of chat
and in the server must be printed all the messages sent by different
clients.
If I run only one client it works fine. In case I run several it only
works well when I am writing in the last client open. If i try to go
back and write in one that was opened before I only can get one line
printed and then it gets blocked. It's quite obvious that it's a
thread problem because it's just paying attention to the last one
opened and not the rest.
Here is the code for the server, cos clients just only use a simple
thread and it seems it works fine.
#include "server.h"

Server::Server()
{
readThread=new Thread(new ThreadStart(this,RunServer));
readThread->Start();
}

void Server::RunServer()
{
TcpListener *listener;

try{
listener=new TcpListener(4500);

listener->Start();

Console::WriteLine(S"Waiting for connection\r\n");

while(true){

connection=listener->AcceptSocket();

if (connection->Connected){
mythread=new Thread(new ThreadStart(this,engine));
mythread->Start();
counter++;
}
}

connection->Close();

}
catch(Exception *error){
MessageBox::Show(error->ToString());
}
}

void Server::engine(){

socketStream=new NetworkStream(connection);

writing=new BinaryWriter(socketStream);
reading=new BinaryReader(socketStream);

Console::WriteLine(S"\r\nConnection {0}
received",counter.ToString());

writing->Write(S"SERVER>>> Connection succesfull");

String *theReply=S"";

do{
try{
theReply=reading->ReadString();

Console::WriteLine(S"\r\n {0}",theReply);

}
catch (Exception *){
break;
}
}while(!theReply->Equals(S"CLIENT>>>
TERMINATE")&&connection->Connected);
socketStream->Close();
writing->Close();
reading->Close();
Console::WriteLine(S"\r\nUser terminated connection");

}

Sorry for my ingnorance about threads. I hope someone helps me. Also I
haven't found any website that explains well threads.

Nov 17 '05 #1
0 759

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
4
by: Julia Goolia | last post by:
hello, i read that it is bad to use threads with tkinter. so my question is how does one create a gui program with sockets? at one point you have to call mainloop() which does not return. ...
0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
14
by: jack | last post by:
At this link I have two c# projects, one is a client, the other is a server. Just point the ip address of the client at the server http://www.slip-angle.com/hosted/bug/ The server does...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
4
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. ...
2
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
11
by: Steven | last post by:
Hi, I need to write an application using sockets. I have a server and about 10 clients "speaking" at the same time with the server, so i guess i need to use asynchronous sockets. But the server...
4
by: nyhetsgrupper | last post by:
Hi, I've written a async server app. This app start by connecting to a client and then send some data (BeginSend). When the data is sent, the server is starting to listen for incomming data....
6
by: Richard | last post by:
Hi All, I don't know in which group my question needs to be posted so here i go: I know that the socket 'begin...' methods uses the threadpool to call the callback function but does that mean...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.