• Advertisement

Archived

This topic is now archived and is closed to further replies.

Winsock

This topic is 5991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I saw the new winsock tutorial up, wow, its great. But my problem is using winsock to connect to a server (asp in my case), I really have no clue and MFC is too much for me at the moment. Anyone? And, as for the code in that tut, that would be for a server running that code right, which means I''d need my own server.... right? I mean is there any free server that would support that, no right?

Share this post


Link to post
Share on other sites
Advertisement
Very quick rundown on Winsock client applications:

To connect to a server at www.myserver.com running on port 1234,
  
// define host

LPHOSTENT host = gethostbyname( "www.myserver.com" );
if( host == NULL ) return -1;
//

// create socket - internet address family, stream, TCP/IP

SOCKET s = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( s == SOCKET_INVALID ) return -1;
//

// set up the internet address structure

SOCKADDR_IN sockaddr;
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr = *( (LPIN_ADDR)*host->h_addr_list );
sockaddr,sin_port = htons( 1234 ); // this is the connection port

//

// connect

int err = connect( s, (LPSOCKADDR)&sockaddr, sizeof(sockaddr) );
if( err == SOCKET_ERROR ) return -1;

// Ready to go!


Look at send() and recv() to see how to transfer data, and closesocket() to disconnect from the server.

Share this post


Link to post
Share on other sites
and what kind of info can I send recieve? I mean, especially teh recieve part, how does it work?

as for send, just ints and chars and other vars, or hwatever the new lingo is?

Share this post


Link to post
Share on other sites
A socket is very much like a file. Anything you can write to / read from a file, you can send/receive on a socket. The only difference is you use send() and recv() instead of fwrite() and fread().


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
but how can you recieve. I mean, ummm.....

ok, I never use fwrite/read I always use cin/out. can someone explain these to me?

Share this post


Link to post
Share on other sites
ignore the recieve part, but I do not know howto use fread/write, I have always used cin/out. Can someone please explain htese briefly or link me to a tutorial.

Share this post


Link to post
Share on other sites
cool, great link, but it seems I''d haveto learna lot in order to be able to be able to send info to a FTP server, so i''m wondering, a programmer of 9years suggested I use ASP w/ winsock, anyone know how that would work with the ASP?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Dean Harding
A socket is very much like a file. Anything you can write to / read from a file, you can send/receive on a socket. The only difference is you use send() and recv() instead of fwrite() and fread().


codeka.com - Just click it.


Actually a socket is a file descriptor. In Unix they''re represented by ints, but Winsock typedefs an unsigned int to give you a socket. Thats why sockets use file read/write operations (same goes for unix pipes).

Actually I wrote an FTP Client implementation, and there was less to it than I though. I can give you the pseudo-code if you wish.

Share this post


Link to post
Share on other sites
please, I mean dear god I''m just lost. If anything I can just use VB for it, but then I''d haveto run my prog through like a VB front end, I''d rather have it part of the prog.

Share this post


Link to post
Share on other sites
Read this:http://www.wu-ftpd.org/rfc/rfc0959.txt

It is the rfc for the File Transfer Protocol. You don''t have to worry about EBCIDIC or structured files since nobody actually supports it.

You need two sockets, the control socket and the data socket. You will need the following functions:

Connect - connects the control socket to the Ftp server on port 21.
WriteToControl - sends strings down the control socket. this is needed to send ftp commands.
ReadFromControl - read strings from the control socket.
Port - sends the port command to the control socket and opens up the data socket on the specified port.
SendFile - uploads a file to the server.
GetFile - gets a file from the server.
Close - ends your ftp session.

Remember to use TCP packets. Hope this gives you a good start.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Actually a socket is a file descriptor. In Unix they''re represented by ints, but Winsock typedefs an unsigned int to give you a socket. Thats why sockets use file read/write operations (same goes for unix pipes).



I know, that''s why I said it In windows, you can even use the WriteFile() and ReadFile() functions to send/receive on a socket. That''s how things like I/O completion ports work with sockets - it basically assumes it''s a file and you can do asynchronous I/O on it.




codeka.com - Just click it.

Share this post


Link to post
Share on other sites

  • Advertisement