⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windows internet programming {part 2}.html

📁 1000 HOWTOs for various needs [WINDOWS]
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<br>&nbsp;

<p>&nbsp; if(listen(fd,BACKLOG) == -1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*

calls listen() */

<br>&nbsp; {

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("listen() error\n");

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<br>&nbsp;

<p>while(1)

<br>&nbsp; {

<br>&nbsp; sin_size=sizeof(struct sockaddr_in);

<p>&nbsp; if ((fd2 = accept(fd,(struct sockaddr *)&amp;client,&amp;sin_size))

== -1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* calls accept()

*/

<br>&nbsp; {

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("accept() error\n");

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<br>&nbsp;

<br>&nbsp; printf("You got a connection from %s\n",inet_ntoa(client.sin_addr)

);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* prints client's IP */

<br>&nbsp;

<br>&nbsp; send(fd2,"Welcome to my server.\n",22,0);&nbsp;&nbsp;&nbsp;

/* send to the client welcome message */

<br>&nbsp;

<br>&nbsp; closesocket(fd2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*&nbsp;

close fd2 */

<p>&nbsp;

<br>&nbsp;&nbsp; WSACleanup();

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -1;

<br>}

<br>}

<p>/* &lt;---- SOURCE CODE ENDS HERE ----> */

<br>&nbsp;

<br>&nbsp;

<p>[ 10.3 ] STREAMING CLIENT

<br>&nbsp;

<br>&nbsp;

<p>/* &lt;---- SOURCE CODE STARTS HERE ----> */

<p>#include &lt;stdio.h>

<br>#include &lt;sys/types.h>

<br>#include &lt;sys/socket.h>

<br>#include &lt;netinet/in.h>

<br>#include &lt;netdb.h>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*

netbd.h is needed for struct hostent =) */

<p>#define PORT 3550&nbsp;&nbsp; /* Open Port on Remote Host */

<br>#define MAXDATASIZE 100&nbsp;&nbsp; /* Max number of bytes of data

*/

<p>int main(int argc, char *argv[])

<br>{

<br>&nbsp; int fd, numbytes;&nbsp;&nbsp; /* files descriptors */

<br>&nbsp; char buf[MAXDATASIZE];&nbsp; /* buf will store received text

*/

<br>&nbsp;

<br>&nbsp; struct hostent *he;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

/* structure that will get information about remote host */

<br>&nbsp; struct sockaddr_in server;&nbsp; /* server's address information

*/

<p>&nbsp; if (argc !=2) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* this is

used because our program will need one argument (IP) */

<br>&nbsp;&nbsp;&nbsp; printf("Usage: %s &lt;IP Address>\n",argv[0]);

<br>&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<p>&nbsp; if ((he=gethostbyname(argv[1]))==NULL){ /* calls gethostbyname()

*/

<br>&nbsp;&nbsp;&nbsp; printf("gethostbyname() error\n");

<br>&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<p>&nbsp; if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){&nbsp; /* calls

socket() */

<br>&nbsp;&nbsp;&nbsp; printf("socket() error\n");

<br>&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<p>&nbsp; server.sin_family = AF_INET;

<br>&nbsp; server.sin_port = htons(PORT); /* htons() is needed again */

<br>&nbsp; server.sin_addr = *((struct in_addr *)he->h_addr);&nbsp; /*he->h_addr

passes "*he"'s info to "h_addr" */

<br>&nbsp; bzero(&amp;(server.sin_zero),8);

<p>&nbsp; if(connect(fd, (struct sockaddr *)&amp;server,sizeof(struct sockaddr))==-1){

/* calls connect() */

<br>&nbsp;&nbsp;&nbsp; printf("connect() error\n");

<br>&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<p>&nbsp; if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1){&nbsp; /* calls

recv() */

<br>&nbsp;&nbsp;&nbsp; printf("recv() error\n");

<br>&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; buf[numbytes]='\0';

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Server Message: %s\n",buf); /*

it prints server's welcome message =) */

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; close(fd);&nbsp;&nbsp; /* close fd =)

*/

<br>}

<p>/* &lt;---- SOURCE CODE ENDS HERE ----> */

<br>&nbsp;

<p>That抯 the source code for the Unix client and of course the windows

one doesn't

<br>look very different.

<br>&nbsp;

<p>/* &lt;---- SOURCE CODE STARTS HERE ----> */

<p>#include &lt;windows.h>

<br>#include &lt;stdio.h>

<p>#define PORT 3550&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Open Port on

Remote Host */

<br>#define MAXDATASIZE 100&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Max number

of bytes of data */

<p>int main(int argc, char *argv[])

<br>{

<br>&nbsp; WSADATA&nbsp; wsdata;

<p>&nbsp; WSAStartup(0x0101,&amp;wsdata);

<p>&nbsp; int fd, numbytes;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* files descriptors

*/

<br>&nbsp; char buf[MAXDATASIZE];&nbsp;&nbsp;&nbsp;&nbsp; /* buf will store

received text */

<br>&nbsp;

<br>&nbsp; struct hostent *he;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

/* structure that will get information about remote host */

<br>&nbsp; struct sockaddr_in server;&nbsp;&nbsp;&nbsp; /* server's address

information */

<p>&nbsp; if (argc !=2) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

/* this is used because our program will need one argument (IP) */

<br>&nbsp;&nbsp;&nbsp; printf("Usage: %s &lt;IP Address>\n",argv[0]);

<br>&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<p>&nbsp; if ((he=gethostbyname(argv[1])) == NULL){ /* calls gethostbyname()

*/

<br>&nbsp;&nbsp;&nbsp; printf("gethostbyname() error\n");

<br>&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<p>&nbsp; if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1) /* calls socket()

*/

<br>&nbsp; {

<br>&nbsp;&nbsp;&nbsp; printf("socket() error\n");

<br>&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<p>&nbsp; server.sin_family&nbsp; = AF_INET;

<br>&nbsp; server.sin_port&nbsp;&nbsp;&nbsp; = htons(PORT);&nbsp;&nbsp;&nbsp;&nbsp;

/* htons() is needed again */

<br>&nbsp; server.sin_addr&nbsp;&nbsp;&nbsp; = *((struct in_addr *)he->h_addr);&nbsp;&nbsp;

/*he->h_addr passes "*he"'s info to "h_addr" */

<br>&nbsp;

<p>&nbsp; if(connect(fd, (struct sockaddr *)&amp;server,sizeof(struct sockaddr))==-1)&nbsp;

/* calls connect() */

<br>&nbsp; {

<br>&nbsp;&nbsp;&nbsp; printf("connect() error\n");

<br>&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<p>&nbsp; if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1)&nbsp;&nbsp;&nbsp;

/* calls recv() */

<br>&nbsp; {

<br>&nbsp;&nbsp;&nbsp; printf("recv() error\n");

<br>&nbsp;&nbsp;&nbsp; exit(-1);

<br>&nbsp; }

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; buf[numbytes] = '\0';

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Server Message: %s\n",buf);&nbsp;&nbsp;&nbsp;

/* it prints server's welcome message =) */

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; closesocket(fd);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

/* close fd =) */

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WSACleanup();

<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -1;

<br>}

<p>/* &lt;---- SOURCE CODE ENDS HERE ----> */

<br>&nbsp;

<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>&nbsp;

<br>&nbsp;

<p>11.0&nbsp;<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>&nbsp;&nbsp; default ports.

<br>&nbsp;

<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>&nbsp;

<br>&nbsp;

<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>&nbsp;

<p>Part 2. -&nbsp; GENERAL SOCKETS&nbsp; - Porting to windows and cross

platform code.

<br>Part 3.&nbsp; -&nbsp; RAW Sockets&nbsp;&nbsp;&nbsp;&nbsp; - Have more

control over packets with raw sockets.

<br>Part 4.&nbsp; -&nbsp; ADVANCED&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - A real

world example by building an internet communication suite.

<br>Part 5.&nbsp; -&nbsp; FINISHING OFF&nbsp;&nbsp;&nbsp; - 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>&nbsp;

<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>&nbsp;

<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 + -