Click to See Complete Forum and Search --> : winsock2
avi123
February 26th, 2004, 04:15 AM
Hello
I'm using winsock2 functions
and I have quite a basic question
if I'm going to connect to the remote computers on 2 ports
I guess I have to get to socket descriptors, right?
I mean I have to call socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
twice
right?
sorry for such a basic question
It's the first time I'm trying to write anything related to tcp/ip communication, etc...
Another thing does anyone have an example of application that connect to a remote computer on more than one port
by creating a thred for each port, and that way working synchronic & blocking?
using winsock2, or maybe just an example code for using winsock2
thanks
avi
Krishnaa
February 26th, 2004, 04:44 AM
Exactly !
for each new connection a new socket is needed. You can resue sockets if you dont communicate parallely.
avi123
February 26th, 2004, 04:56 AM
thank you very much
another question
Should I declare each socket & call connect function in a different thread
or is it ok that I'll do it in the main application and then create for each connection a thread that is waiting for the msg from the remote machine?
thanks again
avi
Krishnaa
February 26th, 2004, 04:59 AM
this totaly depends on your program req. and logic.
But if in general case I will suggest to use diff. threads , create the socket in respective thread itself , or create somewhere and attach it to thread.
Andreas Masur
February 26th, 2004, 05:12 AM
[Moved thread]
Mathew Joy
February 26th, 2004, 05:42 AM
Krishna is right. You need two sockets to connect to service (TCP)running on two different ports. Though the design depends on the requirement, I guess creating 2 sepatate threads is needed one for each connect. Since you are only integrating networking into your program (as I guess) blocking send/recv will be sufficient.
avi123
February 26th, 2004, 06:15 AM
thanks again (both of you)
Do you happen to know how do I make the connection synchronic & blocking?
is it a parameter of the socket function?
I used socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
or mabe of the SOCKADDR_IN structure the I send to the remore machine with the connect function
Mathew Joy
February 26th, 2004, 06:39 AM
The sockets that you create is in the blocking mode by default. You don't have to use anything in particulat. Just use normal send/recv to send and recv data from the server. One thing you have to note that TCP doesn't preserve message boundaries. So you have to format the data that arrives and loop till you send/recv all the data. This is a mistake made commonly by newbies.
:thumb:
simpleman
February 26th, 2004, 08:15 PM
Hi
i am simpleman
As far as I know, the socket created by socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); is non-blocking socket.
CSocket is blocking socket but i don't recommand using CSocket.
Mathew Joy
February 26th, 2004, 11:25 PM
Then what you know is wrong!! Sockets created using socket() call is in blocking mode by default and is overlapped. OTOH CSocket is not blocking either. It only fakes blocking.
simpleman
February 27th, 2004, 12:29 AM
Hi
i am simpleman
i am sorry Mathew, i think you are wrong..
cause sockets created by socket() returns WSAEWOULDBLOCK as a Socket Error. that means sockets created by socket() are working as non blocking method.
Sockets created using socket() call is in blocking mode by default and is overlapped
here, you told Sockets created using socket() call is overlapped.
right and overlapped means non blocking action.
Please reffer to this link http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/socket_2.asp
if your address is right, then tell me how can i make nonblokcing socket.
Mathew Joy
February 27th, 2004, 01:32 AM
When you are corrected check if you are wrong before posting what you 'think'.
If you have MSDN check the page ioctlsocket, where it is written
FIONBIO
The argp parameter is a pointer to an unsigned long value. Set argp to a nonzero value if the nonblocking mode should be enabled, or zero if the nonblocking mode should be disabled. When a socket is created, it operates in blocking mode by default (nonblocking mode is disabled). This is consistent with BSD sockets.
Overlapped socket doesn't mean that it is non-blocking. It means that overlapped operation on the socket is allowed. When using WSARecv()/WSASend() using completion routine or overlapped, it makes the socket into non-blocking. To manually make your socket non-blocking you use.DWORD ul=1;
ret=ioctlsocket(cliSock,FIONBIO,&ul);
if(ret== SOCKET_ERROR)
{
//handle the error
}
avi123
February 29th, 2004, 05:38 AM
Hi,
thanks for all the responses
I don't use "ioctlsocket" function, I use socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1. is it still symetric & blocking by default?
2. if not what parmaters should I change ?
3 .another question: I call recv with a loop, how do I know when the last msg arrived?
4. what do you mean by overlapped sockets
Thanks again
avi
Mathew Joy
February 29th, 2004, 11:24 PM
Originally posted by avi123
Hi,
thanks for all the responses
I don't use "ioctlsocket" function, I use socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1. is it still symetric & blocking by default?
2. if not what parmaters should I change ?
Read my post above. It says to use ioctlsocket to make it non-blocking not blocking. I don't understand what you ment by 'symetric'. As I said you don't have to do anything in particular to make it blocking.
3 .another question: I call recv with a loop, how do I know when the last msg arrived?
See recv is blocking since you are using blocking sockets. That means the call blocks(waits) till you get data on the connection.
4. what do you mean by overlapped sockets
Overlapped sockets allows you to do IO using overlapped operation. Overlapped IO uses overlapped structure to store the various information regarding the overlapped operation. Overlapped IO is asynchronous and so when a overlapped IO is initiated, it'll complete at a latter stage. Overlapped sockets must not be confused with blocking sockets. To do overlapped io, non-blocking sockets is necessary but overlapped sockets doesn't mean that the socket is non-blocking.
avi123
March 1st, 2004, 12:25 AM
thanks
I meant synchronic not symetric, sorry...
so I understand that if I want Symetric I shouldn't use overlapped sockets
Mathew Joy
March 1st, 2004, 12:48 AM
Originally posted by avi123
thanks
I meant synchronic not symetric, sorry...
so I understand that if I want Symetric I shouldn't use overlapped sockets Again 'symetric':rolleyes:?? I don't know why it is hard for you to understand. Go thru the posts and read carefully. And quote the posts if you need additional clarification.
avi123
March 1st, 2004, 01:37 AM
sorry again I meant Synchronic, I don't know why I write symetic all the time, sorry....
so I'll correct, If I work synchronic then I shouldn't use overlapped sockets....
thanks for all your help
avi
codeguru.com
Copyright 2007 Jupitermedia Corporation All Rights Reserved.