473,791 Members | 3,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sockets programming

I've been following a socket programming tutorial to make a simple TCP
communication program, seemingly without hitches, it appears to work fine.
However the structure of it is to have a server listening for requests from
a client using listen(), and when one connects, it communicates with that
client but only that one. It doesn't listen for more requests.

What I'm wondering is can I have a server that continually listens for
requests from multiple clients, and when *any* client connects, it can
communicate with that client. I'm thinking multithreading is obviously going
to be necessary in some form, but basically my questions are:

1) Can the server use a method similar to that described in the second
paragraph above to communicate with multiple clients at the same time on the
same port, or does it have to use a different port for each client that's
simultaneously communicating?
My initial hunch tells me that I can - because a web server can do it, but
is there anything special to watch out for such as how it has to integrate
with (2),...

2) Is it necessary to have multithreading, e.g. the server spawns a new
thread for each client that connects in order to communicate with that
client.

Sorry if I've overly crossposted but this seems to be relevant to MS C++ and
standard C++ as I'm intending to use it in linux aswell as windows, (the
only thing seemingly different in linux seems to be that it has different
includes and doesn't have to call WSAStartup).

Jul 22 '05 #1
11 2213
Bonj <a@b.com> scribbled the following
on comp.lang.c:
I've been following a socket programming tutorial to make a simple TCP
communication program, seemingly without hitches, it appears to work fine.
However the structure of it is to have a server listening for requests from
a client using listen(), and when one connects, it communicates with that
client but only that one. It doesn't listen for more requests.


Neither C or C++ has any socket, TCP, or other networking support
whatsoever. The fact that you are cross-posting to
microsoft.publi c.vc.language looks like you are using Microsoft
Windows. I advise therefore to ask on
comp.os.ms-windows.program mer.win32.
If, by any chance, you are using Unix, ask on comp.unix.progr ammer.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American
Jul 22 '05 #2
Yes.. You can have a server that listens at a desired port for
multiple-clients and communicates using that same port. If you are using
blocking socket, you will have to spawn a worker thread to handle each
client connection.

You probably want to take a look at the following article about blocking and
non-blocking socket :

http://www.developerfusion.co.uk/show/28/8/

TanKC
"Bonj" <a@b.com> wrote in message news:34******** *****@individua l.net...
I've been following a socket programming tutorial to make a simple TCP
communication program, seemingly without hitches, it appears to work fine.
However the structure of it is to have a server listening for requests from a client using listen(), and when one connects, it communicates with that
client but only that one. It doesn't listen for more requests.

What I'm wondering is can I have a server that continually listens for
requests from multiple clients, and when *any* client connects, it can
communicate with that client. I'm thinking multithreading is obviously going to be necessary in some form, but basically my questions are:

1) Can the server use a method similar to that described in the second
paragraph above to communicate with multiple clients at the same time on the same port, or does it have to use a different port for each client that's
simultaneously communicating?
My initial hunch tells me that I can - because a web server can do it, but
is there anything special to watch out for such as how it has to integrate
with (2),...

2) Is it necessary to have multithreading, e.g. the server spawns a new
thread for each client that connects in order to communicate with that
client.

Sorry if I've overly crossposted but this seems to be relevant to MS C++ and standard C++ as I'm intending to use it in linux aswell as windows, (the
only thing seemingly different in linux seems to be that it has different
includes and doesn't have to call WSAStartup).

Jul 22 '05 #3
Ta very much, appreciate it

"TanKC" <kc*****@google .com> wrote in message
news:O7******** ********@TK2MSF TNGP15.phx.gbl. ..
Yes.. You can have a server that listens at a desired port for
multiple-clients and communicates using that same port. If you are using
blocking socket, you will have to spawn a worker thread to handle each
client connection.

You probably want to take a look at the following article about blocking
and
non-blocking socket :

http://www.developerfusion.co.uk/show/28/8/

TanKC
"Bonj" <a@b.com> wrote in message news:34******** *****@individua l.net...
I've been following a socket programming tutorial to make a simple TCP
communication program, seemingly without hitches, it appears to work
fine.
However the structure of it is to have a server listening for requests

from
a client using listen(), and when one connects, it communicates with that
client but only that one. It doesn't listen for more requests.

What I'm wondering is can I have a server that continually listens for
requests from multiple clients, and when *any* client connects, it can
communicate with that client. I'm thinking multithreading is obviously

going
to be necessary in some form, but basically my questions are:

1) Can the server use a method similar to that described in the second
paragraph above to communicate with multiple clients at the same time on

the
same port, or does it have to use a different port for each client that's
simultaneously communicating?
My initial hunch tells me that I can - because a web server can do it,
but
is there anything special to watch out for such as how it has to
integrate
with (2),...

2) Is it necessary to have multithreading, e.g. the server spawns a new
thread for each client that connects in order to communicate with that
client.

Sorry if I've overly crossposted but this seems to be relevant to MS C++

and
standard C++ as I'm intending to use it in linux aswell as windows, (the
only thing seemingly different in linux seems to be that it has different
includes and doesn't have to call WSAStartup).


Jul 22 '05 #4

Off topic for comp.lang.*

However, I suggest you look at some C++ libraries for this sort of thing.

e.g. CommonC++ or ACE.

e.g.
http://www.gnu.org/software/commoncp...p-example.html

I wrote that a while back and I would do it very differently today.

Bonj wrote:
I've been following a socket programming tutorial to make a simple TCP
communication program, seemingly without hitches, it appears to work fine.
However the structure of it is to have a server listening for requests from
a client using listen(), and when one connects, it communicates with that
client but only that one. It doesn't listen for more requests.

What I'm wondering is can I have a server that continually listens for
requests from multiple clients, and when *any* client connects, it can
communicate with that client. I'm thinking multithreading is obviously going
to be necessary in some form, but basically my questions are:

1) Can the server use a method similar to that described in the second
paragraph above to communicate with multiple clients at the same time on the
same port, or does it have to use a different port for each client that's
simultaneously communicating?
My initial hunch tells me that I can - because a web server can do it, but
is there anything special to watch out for such as how it has to integrate
with (2),...

2) Is it necessary to have multithreading, e.g. the server spawns a new
thread for each client that connects in order to communicate with that
client.

Sorry if I've overly crossposted but this seems to be relevant to MS C++ and
standard C++ as I'm intending to use it in linux aswell as windows, (the
only thing seemingly different in linux seems to be that it has different
includes and doesn't have to call WSAStartup).

Jul 22 '05 #5
Bonj wrote:

I've been following a socket programming tutorial to make a simple
TCP communication program, seemingly without hitches, it appears
to work fine. However the structure of it is to have a server
listening for requests from a client using listen(), and when one
connects, it communicates with that client but only that one. It
doesn't listen for more requests.

What I'm wondering is can I have a server that continually listens
for requests from multiple clients, and when *any* client connects,
it can communicate with that client. I'm thinking multithreading
is obviously going to be necessary in some form, but basically my
questions are:

1) Can the server use a method similar to that described in the
second paragraph above to communicate with multiple clients at the
same time on the same port, or does it have to use a different
port for each client that's simultaneously communicating? My
initial hunch tells me that I can - because a web server can do
it, but is there anything special to watch out for such as how it
has to integrate with (2),...

2) Is it necessary to have multithreading, e.g. the server spawns
a new thread for each client that connects in order to communicate
with that client.

Sorry if I've overly crossposted but this seems to be relevant to
MS C++ and standard C++ as I'm intending to use it in linux as
well as windows, (the only thing seemingly different in linux
seems to be that it has different includes and doesn't have to
call WSAStartup).


You are way off topic for c.l.c, and probably for c.l.c++ also,
both of which deal only with the portable ISO standardized
languages (which are not the same). The standard languages do not
contain sockets, multithreading, etc. You should read a newsgroup
for a while before posting into it. You should also set followups
to one group when initially posting a cross-posted inquiry.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Jul 22 '05 #6
Bonj wrote:
I've been following a socket programming tutorial to make a simple TCP communication program, seemingly without hitches, it appears to work fine. However the structure of it is to have a server listening for requests from a client using listen(), and when one connects, it communicates with that client but only that one. It doesn't listen for more requests.

What I'm wondering is can I have a server that continually listens for requests from multiple clients, and when *any* client connects, it can communicate with that client. I'm thinking multithreading is obviously going to be necessary in some form, but basically my questions are:

Yes, more than one way exists to do what you're describing. You could
call listen() and fork on each accept(), or you could use the select()
system call. Forking on accept() generates overhead on process
creation/switching and is generally more cumbersome than using
select(). select() blocks and waits for input/output from user defined
file descriptors. When input/output arrives on one of these file
descriptors, select() returns the file descriptor in question, allowing
for efficient "multiplexi ng" of requests using only a single process.
As always, see select()'s man pages for more information.
1) Can the server use a method similar to that described in the second paragraph above to communicate with multiple clients at the same time on the same port, or does it have to use a different port for each client that's simultaneously communicating?
My initial hunch tells me that I can - because a web server can do it, but is there anything special to watch out for such as how it has to integrate with (2),...


Yes, call the setsockopt() function with the SO_REUSEADDR parameter
set.
setsockopt(SOCK , SOL_SOCKET, SO_REUSEADDR,1)

-dave

Jul 22 '05 #7
Yes, more than one way exists to do what you're describing. You could
call listen() and fork on each accept(), or you could use the select()
system call. Forking on accept() generates overhead on process
creation/switching and is generally more cumbersome than using
select().


Honestly in modern UNIX systems fork()ing doesn't have that much
overhead. It used to be that the entire process space had to be copied
to the new process, but now the page tables are simply marked
copy-on-write, and no actual copying of pages takes place except when
changes occur.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 22 '05 #8

"Bonj" <a@b.com> дÈëÓʼþ news:34******** *****@individua l.net...
I've been following a socket programming tutorial to make a simple TCP
communication program, seemingly without hitches, it appears to work fine.
However the structure of it is to have a server listening for requests from a client using listen(), and when one connects, it communicates with that
client but only that one. It doesn't listen for more requests.

What I'm wondering is can I have a server that continually listens for
requests from multiple clients, and when *any* client connects, it can
communicate with that client. I'm thinking multithreading is obviously going to be necessary in some form, but basically my questions are:

1) Can the server use a method similar to that described in the second
paragraph above to communicate with multiple clients at the same time on the same port, or does it have to use a different port for each client that's
simultaneously communicating?
My initial hunch tells me that I can - because a web server can do it, but
is there anything special to watch out for such as how it has to integrate
with (2),...

2) Is it necessary to have multithreading, e.g. the server spawns a new
thread for each client that connects in order to communicate with that
client.

Sorry if I've overly crossposted but this seems to be relevant to MS C++ and standard C++ as I'm intending to use it in linux aswell as windows, (the
only thing seemingly different in linux seems to be that it has different
includes and doesn't have to call WSAStartup).


There are several ways to do that without multithreading.
You can use select in linux and WaitForMultiple Objects in windows.
Jul 22 '05 #9

"Bonj" <a@b.com> дÈëÓʼþ news:34******** *****@individua l.net...
I've been following a socket programming tutorial to make a simple TCP
communication program, seemingly without hitches, it appears to work fine.
However the structure of it is to have a server listening for requests from a client using listen(), and when one connects, it communicates with that
client but only that one. It doesn't listen for more requests.

What I'm wondering is can I have a server that continually listens for
requests from multiple clients, and when *any* client connects, it can
communicate with that client. I'm thinking multithreading is obviously going to be necessary in some form, but basically my questions are:

1) Can the server use a method similar to that described in the second
paragraph above to communicate with multiple clients at the same time on the same port, or does it have to use a different port for each client that's
simultaneously communicating?
My initial hunch tells me that I can - because a web server can do it, but
is there anything special to watch out for such as how it has to integrate
with (2),...

2) Is it necessary to have multithreading, e.g. the server spawns a new
thread for each client that connects in order to communicate with that
client.

Sorry if I've overly crossposted but this seems to be relevant to MS C++ and standard C++ as I'm intending to use it in linux aswell as windows, (the
only thing seemingly different in linux seems to be that it has different
includes and doesn't have to call WSAStartup).


There are several ways to do that without multithreading.
You can use select in linux and WaitForMultiple Objects in windows.
Jul 22 '05 #10

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

Similar topics

1
2398
by: jubi | last post by:
I am trying to program for a PocketPC using C#. I know it is possible for sockets programming for a desktop using C#. I'd like to know if it's possible to: 1. Sockets programming for the PocketPC and 2. Using C# Links to more reading/resources are greatly appreciated!
4
2168
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I create a socket, bind it to a port, put it in listen mode, and then n sockets can connect to it. The code: Socket newSocket = listeningSocket.Accept(); returns a socket. I can communicate on newSocket, and listeningSocket goes
6
7003
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
1
1108
by: Sougato Das | last post by:
Does anyone know any good places for information on sockets programming using VB.NET. I know VB.NET fairly well, but I know very little about sockets. Thanks.
4
6321
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
4364
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly found out that .net uses system.net.sockets.socket or .tcpclient/.tcpserver. and these confuse me... :o| and help would be really great! links, code, wrappers, tips, whateveryougot... one of my main problems, i guess, is: why don't sockets...
2
3315
by: djc | last post by:
I am very new to this... I just started playing around with some network programming. I really only use vb.net. I say that so I don't get the 'use c/c++' answer. Programming is a hobby for me, not a career. I'm only interested, at least right now, in playing around with vb.net and maybe some c#.net. I'm not sure if this is the right forum or not but... my first question is to make sure I understand something: 1) the main difference...
1
1451
by: Farslayer | last post by:
I'm trying to find a good source for programming sockets with VB.NET. The examples in the MSDN help do not show how to accept incoming connections and add this connection to a local array or something similar. Basically, my application will accept a connection, but the client, after the connection is accepted, must authenticate; if it does not, the connection is dropped. But, the sockets are asynchronous and there can be up to 25...
2
6672
by: a_agaga | last post by:
Do you know are there some reasons why many do not make processes to communicate through memory? Why network connections (sockets) are used so commonly in IPC (inter process communication) instead of memory? (Is IPC harder to maintain / handle if it is made through memory, when compared to communication through sockets?) Some background things:
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.