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

📄 windows internet programming {part 2}.html

📁 1000 HOWTOs for various needs [WINDOWS]
💻 HTML
📖 第 1 页 / 共 3 页
字号:
	| Unix Function | Windows Function |

	+===============+==================+

	|    close()    |   closesocket()  |

	+---------------+------------------+

	|    ioctl()    |   ioctlsocket()  |

	+---------------+------------------+

	|     read()    |      recv()      |

	+---------------+------------------+

	|    write()    |      send()      |

	+---------------+------------------+

</pre>

<p>&nbsp;FIG 1. - Comparison of Windows and Unix Socket functions.

<br>&nbsp;

<p>

<hr SIZE=1 WIDTH="70%">

<br>&nbsp;

<p>8.0<a NAME="8"></a> BLOCKING ROUTINES

<br>=======================================

<p>In windows you can use blocking routines but they are not recommended.

In windows 3.1

<br>using the event driven paradigm it is possible to have a blocked operation

disturbed

<br>by other event driven activity.

<p>Soon you will not be able to have blocking routines in Winsock applications

so it is

<br>not recommended you use them at all in this scenario as this would

limit distribution

<br>possibilities.

<br>&nbsp;

<p>

<hr SIZE=1 WIDTH="70%">

<br>&nbsp;

<p>9.0&nbsp;<a NAME="9"></a>ADDITIONAL FUNCTIONS

<br>=======================================

<p>In windows there are some functions which aren't seen in UNIX sockets.

In every

<br>windows socket program you must call WSAStarup() before you can call

any socket

<br>functions or use any code.

<p>Additionally every WSAStartup() function has a corresponding WSACleanup()

which

<br>shuts down the Winsock and cleans up after us.

<p>Every Windows socket program must contain both of these functions.

<br>&nbsp;

<p>

<hr SIZE=1 WIDTH="70%">

<br>&nbsp;

<p>10.0&nbsp;<a NAME="10"></a>PORTING CODE

<br>=======================================

<p>Now that we have discussed the different parts of the program in Unix

and its relevant

<br>Windows parts we can look at some real code.

<br>&nbsp;

<br>&nbsp;

<p>[ 10.1 ]<a NAME="10.1"></a> DNS PROGRAM

<p>The first program we come across is Bracamans DNS application. Here抯

the Unix Code.

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

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

<br>#include &lt;netdb.h>&nbsp;&nbsp; /* This is the header file needed

for gethostbyname() */

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

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

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

<br>&nbsp;

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

<br>{

<br>&nbsp; struct hostent *he;

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

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

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

<br>&nbsp; }

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

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

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

<br>&nbsp; }

<p>&nbsp; printf("Hostname : %s\n",he->h_name);&nbsp; /* prints the hostname

*/

<br>&nbsp; printf("IP Address: %s\n",inet_ntoa(*((struct in_addr *)he->h_addr)));

/* prints IP address */

<br>}

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

<br>&nbsp;

<br>&nbsp;

<p>This is a very small but still a very useful program. If we attempt

to compile this

<br>right away under Microsoft Visual C++ We will get an error saying that

it can not

<br>find an include file, so our first duty is to remove all of the Unix

Socket include

<br>files and replace them with the line:

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

<p>remembering to leave stdio.h right where it is.

<br>When I tried compiling it now I got several more errors so I changed

the

<br>main() fuction from what it is above to:

<p>void main(int argc, char **argv) and the program then compiled.

<p>However I then got several linker errors. Remember when socket programming

on windows

<br>you must link to the file Wsock32.lib.

<p>After I linked to the proper .lib file the program compiles perfectly,

without the

<br>need to touch a single line of the socket code!!

<p>This is how compatible different platforms can be with socket code,

however this is a

<br>very small program and this is not usually the case. The following

is the windows

<br>source code for the port of Bracamans DNS program.

<p>Remember you will get an error unless you include WSAStartup() and WSACleanup()!!

<br>&nbsp;

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

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

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

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

<br>{

<p>&nbsp; WSADATA&nbsp; wsdata;

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

<p>&nbsp; struct hostent *he;

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

<br>&nbsp; {

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

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

<br>&nbsp; }

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

<br>&nbsp; {

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

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

<br>&nbsp; }

<p>&nbsp; printf("Hostname : %s\n",he->h_name);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

/* prints the hostname */

<br>&nbsp; printf("IP Address: %s\n",inet_ntoa(*((struct in_addr *)he->h_addr)));&nbsp;

/* prints IP address */

<p>&nbsp; WSACleanup();

<br>&nbsp; return -1;

<p>}

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

<br>&nbsp;

<p>This program has almost as few lines as the UNIX alternative and requires

practically

<br>no porting at all!

<p>Like I said you wouldn抰 usually have a program that runs on windows

with such little altering.

<br>Lets discuss porting Bracamans TCP streaming server.

<br>&nbsp;

<p>[ 10.2 ]&nbsp;<a NAME="10.2"></a>STREAMING SERVER

<br>&nbsp;

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

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

/* These are the usual header files */

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

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

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

<br>&nbsp;

<p>#define PORT 3550&nbsp;&nbsp; /* Port that will be opened */

<br>#define BACKLOG 2&nbsp;&nbsp; /* Number of allowed connections */

<p>main()

<br>{

<br>&nbsp;

<br>&nbsp; int fd, fd2; /* file descriptors */

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

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

<p>&nbsp; int sin_size;

<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);&nbsp;&nbsp; /* Remember htons()

from "Conversions" section? =) */

<br>&nbsp; server.sin_addr.s_addr = INADDR_ANY;&nbsp; /* INADDR_ANY puts

your IP address automatically */

<br>&nbsp; bzero(&amp;(server.sin_zero),8); /* zero the rest of the structure

*/

<p>&nbsp;

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

/* calls bind() */

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

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

<br>&nbsp; }

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

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

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

<br>&nbsp; }

<p>while(1){

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

<br>&nbsp; if ((fd2 = accept(fd,(struct sockaddr *)&amp;client,&amp;sin_size))==-1){

/* calls accept() */

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

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

<br>&nbsp; }

<br>&nbsp;

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

); /* prints client's IP */

<br>&nbsp;

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

welcome message */

<br>&nbsp;

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

<br>}

<br>}

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

<br>&nbsp;

<br>&nbsp;

<p>Well lets try and compile shall we...

<br>&nbsp;

<p>Straight away we should clear the obvious problems, link to Winsock32.lib

and

<br>only include windows.h and stdio.h.

<p>Having done so we receive 2 errors, the first pertains to bzero which

we do not

<br>have in our little program so we are going to just remove the line

that contains

<br>it. Once we do so we now have only 1 error, woohoo!

<p>This error tells us that the function close() is not a valid identifier

which is

<br>what I said earlier, close() has been replaced by closesocket() in

windows so we

<br>change that as well to closesocket().

<p>We run this program and we recieve a socket error :(. But remember we

never entered

<br>WSAStartup() and WSACleanup() in this program so we have to do that

first and then

<br>our program will work :).

<p>If you run the program the command prompt should remain blank, just

leave the program

<br>as it is now and move onto the next section to figure out where to

go from here!!

<p>First of course here抯 the Windows code for the port ;).

<br>&nbsp;

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

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

/* These are the usual header files */

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

<br>&nbsp;

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

Port that will be opened */

<br>#define BACKLOG 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*

Number of allowed connections */

<p>main()

<br>{

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

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

<p>&nbsp; int fd, fd2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* file

descriptors */

<br>&nbsp;

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

address information */

<br>&nbsp; struct sockaddr_in client;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*

client's address information */

<p>&nbsp; int sin_size;

<br>&nbsp;

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

/* 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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = AF_INET;

<br>&nbsp; server.sin_port&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

= htons(PORT);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Remember htons() from "Conversions"

section? =) */

<br>&nbsp; server.sin_addr.s_addr&nbsp; = INADDR_ANY;&nbsp;&nbsp;&nbsp;&nbsp;

/* INADDR_ANY puts your IP address automatically */

<p>&nbsp;

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

/* calls bind() */

<br>&nbsp; {

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

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

<br>&nbsp; }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -