套接字编程 [英] Sockets programming

查看:77
本文介绍了套接字编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注套接字编程教程,制作一个简单的TCP

通信程序,看似没有障碍,看起来工作正常。

但结构它是让服务器使用listen()监听来自

a客户端的请求,当一个连接时,它与那个

客户端通信,但只与那个客户端通信。它没有听取更多的请求。


我想知道的是我能不能一直监听来自多个客户的
请求的服务器,当*任何*客户端连接时,它可以

与该客户端通信。我认为多线程显然需要以某种形式支持

,但基本上我的问题是:


1)服务器是否可以使用方法类似于上面第二个

段落中描述的那个,在

相同的端口上同时与多个客户端通信,或者是否必须为每个端口使用不同的端口同时沟通的客户是什么?

我的初步预感告诉我,我可以 - 因为网络服务器可以做到,但是

有什么特别值得注意的,例如它如何整合

和(2),...


2)是否有必要多线程,例如服务器为每个连接的客户端生成一个新的

线程,以便与那个

客户端进行通信。


对不起,如果我'过度交叉,但这似乎与MS C ++和

标准C ++相关,因为我打算在linux中使用它以及windows,(

在linux中只有看似不同的东西似乎是它有不同的

包括而且不必调用WSAStartup。

解决方案

Bonj< a@b.com>在comp.lang.c上潦草地写下以下

我一直在关注套接字编程教程,制作一个简单的TCP
通信程序,看似没有但它的结构似乎工作正常。
然而,它的结构是让服务器使用listen()监听来自客户端的请求,当一个连接时,它与之通信
客户但只有那一个。它不会收听更多请求。




C或C ++都没有任何套接字,TCP或其他网络支持

无论如何。您正在交叉发布到

microsoft.public.vc.language这一事实看起来像是在使用Microsoft

Windows。因此我建议在

comp.os.ms-windows.programmer.win32上询问。

如果您使用Unix,请询问comp.unix .programmer。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)------ -------芬兰-------- \

\ ---------------------- --------------------------------规则! -------- /

这是一个人员通勤者。

- 科学美国人的火车司机


是..您可以让服务器在所需的端口上侦听多个客户端,并使用相同的端口进行通信。如果您使用

阻塞套接字,则必须生成一个工作线程来处理每个

客户端连接。


你可能想看看下面有关阻止和

非阻塞套接字的文章:

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


TanKC

" Bonj" < a@b.com>在消息新闻中写道:34 ************* @ individual.net ...

我一直在关注套接字编程教程来制作一个简单的TCP <通信程序,似乎没有障碍,似乎工作正常。
然而,它的结构是让服务器使用listen()从客户端侦听请求
,当一个连接时,它与那个客户沟通,但只与那个客户沟通。它并没有听取更多的请求。

我想知道的是,我可以让服务器不断收听来自多个客户端的请求吗?当* any * client连接时,它可以与该客户进行通信。我认为多线程显然需要某种形式的
,但基本上我的问题是:1)服务器是否可以使用类似于第二种方法中描述的方法<以上段落在
同一端口同时与多个客户端进行通信,或者是否必须为每个同时进行通信的客户端使用不同的端口?
我最初的预感告诉我,我可以 - 因为一个网络服务器可以做到这一点,但
有什么特别需要注意的,例如它如何与(2)整合
,...... br />
2)是否有必要进行多线程处理,例如服务器为每个连接的客户端生成一个新的
线程,以便与该客户端进行通信。

对不起,如果我已经过度交叉,但这似乎与MS C ++
和标准C ++因为我打算在linux中使用它以及windows,(在linux中看似不同的东西似乎是它有不同的
包含和不不必调用WSAStartup。



Ta非常感谢,


" TanKC" < KC ***** @ google.com>在消息中写道

新闻:O7 **************** @ TK2MSFTNGP15.phx.gbl ...

是..您可以让服务器在所需的端口上侦听多个客户端,并使用相同的端口进行通信。如果您使用
阻塞套接字,则必须生成一个工作线程来处理每个客户端连接。

您可能想看看以下关于阻塞的文章

非阻塞插座:

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

TanKC

" Bonj" < a@b.com>在消息新闻中写道:34 ************* @ individual.net ...

我一直在关注套接字编程教程来制作一个简单的TCP <通信程序,似乎没有障碍,它似乎工作
很好。
然而它的结构是让服务器监听请求


来自

一个使用listen()的客户端,当一个连接时,它与该客户端进行通信,但只与该客户端进行通信。它并没有听取更多的请求。

我想知道的是,我可以让服务器不断收听来自多个客户端的请求吗?当* any * client连接时,它可以与该客户进行通信。我认为多线程显然是


以某种形式进行

,但基本上我的问题是:

1)可以服务器使用类似于上面第二段中描述的方法,在

相同的端口上同时与多个客户端进行通信,或者是必须为每个同时进行通信的客户端使用不同的端口?
我最初的预感告诉我,我可以 - 因为网络服务器可以做到这一点,
但是
有什么特别值得注意的,例如如何整合
与(2),...

2)是否有必要进行多线程处理,例如服务器为每个连接的客户端生成一个新的
线程,以便与该客户端进行通信。

对不起,如果我已经过度交叉,但这似乎与MS C ++


标准C ++因为我打算在linux中使用它以及windows,(在linux中似乎只有看似不同的东西)是它有不同的
包括,并没有必要调用WSAStartup。




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).

解决方案

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.public.vc.language looks like you are using Microsoft
Windows. I advise therefore to ask on
comp.os.ms-windows.programmer.win32.
If, by any chance, you are using Unix, ask on comp.unix.programmer.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American


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*************@individual.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).



Ta very much, appreciate it

"TanKC" <kc*****@google.com> wrote in message
news:O7****************@TK2MSFTNGP15.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*************@individual.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).




这篇关于套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆