📄 windows internet programming {part 2}.html
字号:
<br>
<p> if(listen(fd,BACKLOG) == -1) /*
calls listen() */
<br> {
<br> printf("listen() error\n");
<br> exit(-1);
<br> }
<br>
<p>while(1)
<br> {
<br> sin_size=sizeof(struct sockaddr_in);
<p> if ((fd2 = accept(fd,(struct sockaddr *)&client,&sin_size))
== -1) /* calls accept()
*/
<br> {
<br> printf("accept() error\n");
<br> exit(-1);
<br> }
<br>
<br> printf("You got a connection from %s\n",inet_ntoa(client.sin_addr)
); /* prints client's IP */
<br>
<br> send(fd2,"Welcome to my server.\n",22,0);
/* send to the client welcome message */
<br>
<br> closesocket(fd2); /*
close fd2 */
<p>
<br> WSACleanup();
<br> return -1;
<br>}
<br>}
<p>/* <---- SOURCE CODE ENDS HERE ----> */
<br>
<br>
<p>[ 10.3 ] STREAMING CLIENT
<br>
<br>
<p>/* <---- SOURCE CODE STARTS HERE ----> */
<p>#include <stdio.h>
<br>#include <sys/types.h>
<br>#include <sys/socket.h>
<br>#include <netinet/in.h>
<br>#include <netdb.h> /*
netbd.h is needed for struct hostent =) */
<p>#define PORT 3550 /* Open Port on Remote Host */
<br>#define MAXDATASIZE 100 /* Max number of bytes of data
*/
<p>int main(int argc, char *argv[])
<br>{
<br> int fd, numbytes; /* files descriptors */
<br> char buf[MAXDATASIZE]; /* buf will store received text
*/
<br>
<br> struct hostent *he;
/* structure that will get information about remote host */
<br> struct sockaddr_in server; /* server's address information
*/
<p> if (argc !=2) { /* this is
used because our program will need one argument (IP) */
<br> printf("Usage: %s <IP Address>\n",argv[0]);
<br> exit(-1);
<br> }
<p> if ((he=gethostbyname(argv[1]))==NULL){ /* calls gethostbyname()
*/
<br> printf("gethostbyname() error\n");
<br> exit(-1);
<br> }
<p> if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){ /* calls
socket() */
<br> printf("socket() error\n");
<br> exit(-1);
<br> }
<p> server.sin_family = AF_INET;
<br> server.sin_port = htons(PORT); /* htons() is needed again */
<br> server.sin_addr = *((struct in_addr *)he->h_addr); /*he->h_addr
passes "*he"'s info to "h_addr" */
<br> bzero(&(server.sin_zero),8);
<p> if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){
/* calls connect() */
<br> printf("connect() error\n");
<br> exit(-1);
<br> }
<p> if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1){ /* calls
recv() */
<br> printf("recv() error\n");
<br> exit(-1);
<br> }
<p> buf[numbytes]='\0';
<p> printf("Server Message: %s\n",buf); /*
it prints server's welcome message =) */
<p> close(fd); /* close fd =)
*/
<br>}
<p>/* <---- SOURCE CODE ENDS HERE ----> */
<br>
<p>That抯 the source code for the Unix client and of course the windows
one doesn't
<br>look very different.
<br>
<p>/* <---- SOURCE CODE STARTS HERE ----> */
<p>#include <windows.h>
<br>#include <stdio.h>
<p>#define PORT 3550 /* Open Port on
Remote Host */
<br>#define MAXDATASIZE 100 /* Max number
of bytes of data */
<p>int main(int argc, char *argv[])
<br>{
<br> WSADATA wsdata;
<p> WSAStartup(0x0101,&wsdata);
<p> int fd, numbytes; /* files descriptors
*/
<br> char buf[MAXDATASIZE]; /* buf will store
received text */
<br>
<br> struct hostent *he;
/* structure that will get information about remote host */
<br> struct sockaddr_in server; /* server's address
information */
<p> if (argc !=2) {
/* this is used because our program will need one argument (IP) */
<br> printf("Usage: %s <IP Address>\n",argv[0]);
<br> exit(-1);
<br> }
<p> if ((he=gethostbyname(argv[1])) == NULL){ /* calls gethostbyname()
*/
<br> printf("gethostbyname() error\n");
<br> exit(-1);
<br> }
<p> if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1) /* calls socket()
*/
<br> {
<br> printf("socket() error\n");
<br> exit(-1);
<br> }
<p> server.sin_family = AF_INET;
<br> server.sin_port = htons(PORT);
/* htons() is needed again */
<br> server.sin_addr = *((struct in_addr *)he->h_addr);
/*he->h_addr passes "*he"'s info to "h_addr" */
<br>
<p> if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1)
/* calls connect() */
<br> {
<br> printf("connect() error\n");
<br> exit(-1);
<br> }
<p> if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1)
/* calls recv() */
<br> {
<br> printf("recv() error\n");
<br> exit(-1);
<br> }
<p> buf[numbytes] = '\0';
<p> printf("Server Message: %s\n",buf);
/* it prints server's welcome message =) */
<p> closesocket(fd);
/* close fd =) */
<p> WSACleanup();
<br> return -1;
<br>}
<p>/* <---- SOURCE CODE ENDS HERE ----> */
<br>
<p>There抯 actually no real difference with this port and the last. The
same lines are
<br>changed and the same functions are included as before.
<p>Start the program by switching to its directory on the command line
and typing
<p>streamclient.exe local host
<p>Where streamclient.exe is the name of the stream client program we just
ported
<br>naturally :).
<p>Just make sure that the previous example, the streaming server is running
and
<br>your away. The screen will print the line "Welcome to my server." Congratulations
<br>you抳e ported your first Unix Socket client and server to windows and
you received
<br>a line of text back from your own server :).
<p>
<hr SIZE=1 WIDTH="70%">
<br>
<br>
<p>11.0 <a NAME="11"></a>Planning
<br>=======================================
<p>When porting its probably best to go trough things like a check list
before attempting
<br>to compile the bloody thing. Here抯 a list of some recommendations
to run trough first.
<p>1. Replace all network header files with windows.h or winsock.h.
<br>2. Make sure to add WSAStartup() and WSACleanup() before and after
the code.
<br>3. Replace functions such as close() and ioctl() with windows counterparts.
<br>4. Take care of problems such as bzero() and add any windows specific
features like
<br> default ports.
<br>
<p>With so few and simple operations like this it would be pretty damn
easy to write
<br>a program that performs these actions for you, I doubt it would be
complete but hey
<br>it would save you a lot of time in the long run, especially with larger
applications.
<p>
<hr SIZE=1 WIDTH="70%">
<br>
<br>
<p>12.0<a NAME="12"></a> LAST WORDS
<br>==============
<p>Well that抯 it for this tutorial, or at least this part of it. More
parts are planned so
<br>hopefully you should see them on blacksun soon :). This part covered
the basics of
<br>windows programming, there was no point in writing about UNIX programming
as bracaman
<br>has released a very good tutorial on basic UNIX sockets programming
which is available
<br>from blacksun.box.sk and code.box.sk.
<p>I will be discussing Unix sockets again however in the next part of
this tutorial but
<br>mostly in a porting sense. To see what抯 planned for future parts so
far just look down :).
<br>
<p>Part 2. - GENERAL SOCKETS - Porting to windows and cross
platform code.
<br>Part 3. - RAW Sockets - Have more
control over packets with raw sockets.
<br>Part 4. - ADVANCED - A real
world example by building an internet communication suite.
<br>Part 5. - FINISHING OFF - Putting it
all together in one big program.
<p>So that抯 what抯 still to come, but I may change some of these future
tutorials at any
<br>time before there released if I do so it will be to improve the series
in my view and
<br>remember send me comments and suggestions and your ideas might also
change the layout
<br>of following tutorials.
<br>
<p>Well thank you for reading this tutorial and visit my website to keep
up to date with
<br>the series and for add on code and articles to the series.
<p>- BR ;)
<br>
<p>*<a NAME="final"></a>FINAL NOTE*
<p>Id really like to thank Bracaman for everything including allowing me
to use code from
<br>his tutorial and please remember that this tutorial should be read
with his (after his
<br>1 even :P), so thanks for everything Bracaman it helped so much :).
<p>
<hr SIZE=1 WIDTH="70%">
<p>APPENDIX A - THE COMPILER<a NAME="appxA"></a>
<br>
<p>All of the programs in this tutorial have been tested under Microsoft
Visual C++ 6.0,
<br>im running Windows 2000 Professional edition and I have an Intel Pentium
2.
<p>To set up the compiler for Winsock programming you need to link Wsock32.lib
to your project.
<br>Under Visual C++ do the following.
<p>1. Select the File view tab.
<br>2. Right Click the files menu and go to Settings.
<br>3. Select the Link tab in Project Settings.
<br>4. Add Wsock32.lib to the list of .lib files and press ok.
<p>The winsock.h header file is included in windows.h so don抰 worry about
that.
<p>
<hr SIZE=1 WIDTH="70%">
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -