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

📄 windows internet programming {part 2}.html

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


<html>

<head>

     <title>Windows Internet Programming Part2</title>

</head>

<body>

<pre>

<font color=red>

              _________________

             /_               /\  

              \/  _______    /  \

              /  /      /   /   /

             /  /______/   /   /

            /           __/   /

           /  _______   \  __/

          /  /      /   /  \

         /  /______/   /   / 

       _/             /   /      

      /______________/   /       BLACK SUN RESEARCH FACILITY

      \              \  /      	   <a href="http://blacksun.box.sk">HTTP://BLACKSUN.BOX.SK</a>

       \______________\/





</font>





WINDOWS INTERNET PROGRAMMING {part 2}

=================================================







   WRITTEN BY                 [ <a href="mailto:cos125@hotmail.com">cos125@hotmail.com</a>                :E-MAIL    ]      

           <a href="mailto:cos125@hotmail.com">BINARY RAPE</a>        [ 114603188                         :ICQ#      ]      

                              [ <a href="http://www.geocities.com/wininetprogram">www.geocities.com/wininetprogram</a>  :WEB SITE  ]

                              [ <a href="http://blacksun.box.sk">http://blacksun.box.sk</a>            :TURORIALS ]      











Thanks to cyberwolf for letting me write this and BSRF for releasing it.











<br>

<p>Disclaimer

<p>None of the information or code in this tutorial is meant to be used

against others

<br>or to purposely damage computer systems and/or cause any loss of or

damage to property.

<p>Further more, neither myself or any other contributor to, or member

of, the Blacksun

<br>research Facility (BSRF) can be held responsible for damage or loss

of property to

<br>computer systems as a result of this tutorial.

<p>In this tutorial the code is provided as a learning aid so you can see

how its done

<br>its not meant for you to use against yourself or others.

<p>If you don't agree with any of this then please stop reading......&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

now!

<br>

<p>CONTENTS

<br>

<p>1.&nbsp; <a href="#1">Introduction</a>

<br>2.&nbsp; <a href="#2">UNIX and Windows</a>

<br>3.&nbsp; <a href="#3">Error Codes</a>

<br>4.&nbsp; <a href="#4">Port Numbers</a>

<br>5.&nbsp; <a href="#5">Include Files</a>

<br>&nbsp;&nbsp; -<a href="#5.1"> 5.1 socket()</a>

<br>&nbsp;&nbsp;&nbsp; -<a href="#5.2">5.2 bind()</a>

<br>&nbsp;&nbsp;&nbsp; -<a href="#5.3">5.3 connect()</a>

<br>&nbsp;&nbsp;&nbsp; -<a href="#5.4">5.4 listen()</a>

<br>&nbsp;&nbsp;&nbsp; -<a href="#5.5">5.5 accept()</a>

<br>&nbsp;&nbsp;&nbsp; -<a href="#5.6">5.6 gethostname()</a>

<p>6.&nbsp; <a href="#6">Common Functions</a>

<br>7.&nbsp;<a href="#7"> Renamed Functions</a>

<br>8.&nbsp; <a href="#8">Blocking Routines</a>

<br>9.&nbsp; <a href="#9">Additional Functions</a>

<br>10. <a href="#10">Porting Code</a>

<br>&nbsp;&nbsp;&nbsp; -10.1 <a href="#10.1">DNS Program</a>

<br>&nbsp;&nbsp;&nbsp; -10.2 <a href="#10.2">Streaming Server</a>

<br>&nbsp;&nbsp;&nbsp; -10.3 Streaming Client

<p>11. <a href="#11">Planning</a>

<br>12. <a href="#12">Last Words</a>

<p>&nbsp;&nbsp;&nbsp; APPENDIXES

<p>&nbsp;&nbsp;&nbsp; A - <a href="#appxA">The Compiler</a>

<p>______________________

<br>1.0<a NAME="1"></a> INTRODUCTION

<br>&nbsp;

<p>In the last part in this series of tutorials I discussed windows sockets

programming using

<br>the winsock API and in that document I mentioned that the windows Sockets

implementation is

<br>based on the Berkeley Sockets idea, therefore socket programming on

systems such as UNIX

<br>and Linux, which are also based on the Berkeley API would be quiet

similar. This aids us

<br>in porting from platform to platform, making it easy to move whole

programs from UNIX to

<br>windows in a very short time.

<p>This tutorial will discuss and illustrate the differences in code and

functions between the

<br>platforms and sample source code and applications will be provided

to show how to port the

<br>applications piece by piece.

<p>I suggest that you first read Part 1 in this series of tutorials and

also Bracaman's basic

<br>UNIX sockets tutorial, also available from blacksun.box.sk and also

from code.box.sk.

<p>Any add on information for this tutorial and updates are available as

usual from:

<br>http://www.geocities.com/winnetprogram.

<br>&nbsp;

<p>

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

<br>&nbsp;

<p>2.0&nbsp;<a NAME="2"></a>UNIX AND WINDOWS

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

<p>Unix and Windows handle sockets differently, UNIX treats sockets the

same as a file handle

<br>which is a small integer while in windows it is an unsigned type called

SOCKET.

<p>So from the start UNIX and windows actually think of there respective

sockets differently.

<p>To best explain the differences the following is an exert from bracamans

tutorial and

<br>describes sockets from the Unix point of view:

<br>&nbsp;

<p>"In a very simple way, a socket is a way to talk to other computer.

<p>To be more precise, it's a way to talk to other computers using standard

Unix file

<br>descriptors.

<p>In Unix, every I/O actions are done by writing or reading to a file

descriptor. A file

<br>descriptor is just an integer associated with an open file and it can

be a network connection,

<br>a terminal, or something else."

<br>&nbsp;

<p>

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

<br>&nbsp;

<p>3.0&nbsp;<a NAME="3"></a>ERROR CODES

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

<p>In UNIX error codes are available trough the errno variable. In windows

we use the function

<br>WSAGetLastError() to obtain these error codes.

<br>&nbsp;

<p>_________________________

<p>4.0&nbsp;<a NAME="4"></a>PORT NUMBERS

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

<p>In both Unix and Windows you define the port numbers by entering the

destination port number

<br>as a parameter to the function htons(), however in winsock.h several

port numbers are defined

<br>for common ports so that you can also use these.

<p>The following is a list of the default names and there relative port

numbers:

<br>

<pre>



1.  IPPORT_ECHO             -  7

2.  IPPORT_DISCARD          -  9

3.  IPPORT_SYSTAT           -  11

4.  IPPORT_DAYTIME          -  13

5.  IPPORT_NETSTAT          -  15

6.  IPPORT_FTP              -  21

7.  IPPORT_TELNET           -  23

8.  IPPORT_SMTP             -  25

9.  IPPORT_TIMESERVER       -  37

10. IPPORT_NAMESERVER       -  42

11. IPPORT_WHOIS            -  43

12. IPPORT_MTP              -  57

</pre>

<p>

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

<br>&nbsp;

<p>5.0&nbsp;<a NAME="5"></a>INCLUDE FILES

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

<p>In UNIX there are a number of include files to use in socket programming,

following is a

<br>list of these functions and related #include files:

<br>&nbsp;

<p>5.1&nbsp;<a NAME="5.1"></a>socket()

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ #include &lt;sys/types.h>&nbsp; ]

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

<br>&nbsp;

<p>5.2&nbsp;<a NAME="5.2"></a>bind()

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ #include &lt;sys/types.h>&nbsp; ]

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

<br>&nbsp;

<p>5.3&nbsp;<a NAME="5.3"></a>connect()

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ #include &lt;sys/types.h>&nbsp; ]

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

<br>&nbsp;

<p>5.4&nbsp;<a NAME="5.4"></a>listen()

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ #include &lt;sys/types.h>&nbsp; ]

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

<br>&nbsp;

<p>5.5&nbsp;<a NAME="5.5"></a>accept()

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ #include &lt;sys/socket.h> ]

<p>5.6&nbsp;<a NAME="5.6"></a>gethostname()

<p>&nbsp; [ #include &lt;netdb.h>&nbsp;&nbsp; ]

<br>&nbsp;

<br>&nbsp;

<p>You will need some general socket include files also in UNIX programming:

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

<br>#include &lt;arpa/inet.h>

<br>&nbsp;

<p>Your average list of include files at the top of a UNIX socket program

would therefore

<br>look something like the following:

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

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

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

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

<br>#include &lt;arpa/inet.h>

<br>&nbsp;

<p>In windows the header file for socket and internet programming is contained

in windows.h,

<br>therefore the top of a windows socket programming would look like this:

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

<p>The name of the header file for windows socket programming is winsock.h

and including

<br>this file in your project as well wont cause any errors of course but

there抯 just no need.

<p>Windows programs also need to be linked to the file Wsock32.lib before

they can be

<br>compiled.

<p>Consult your compilers documents for further information on this or

proceed to Appendix 1

<br>- The compiler.

<p>

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

<br>

<p>6.0<a NAME="6"></a>COMMON FUNCTIONS

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

<p>Both Unix and Windows sockets have a set of functions common to both

platforms. These are

<br>mostly the functions dealing with TCP and UDP and all conversion functions

and structures

<br>are the same for both platforms.

<p>For example htons() and inet_addr() are the same on both platforms,

also so is the

<br>structures sockaddr and sockaddr_in.

<br>&nbsp;

<p>Here is a list of common functions.

<p>1.&nbsp; Socket()

<p>2.&nbsp; Bind()

<p>3.&nbsp; Listen()

<p>4.&nbsp; Connect()

<p>5.&nbsp; Accept()

<p>6.&nbsp; Send()

<p>7.&nbsp; Recv()

<p>8.&nbsp; Sendto()

<p>9.&nbsp; Recvfrom()

<p>10. Gethostname()

<br>&nbsp;

<p>

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

<br>&nbsp;

<p>7.0<a NAME="7"></a> RENAMED FUNCTIONS

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

<p>There are some functions which exist in both windows and UNIX socket

programming but

<br>during the conversion the names have changed slightly.

<p>The following table contains a comparison of renamed functions in windows

and UNIX.<br>

<pre>



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

⌨️ 快捷键说明

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