📄 windows internet programming {part 2}.html
字号:
<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......
now!
<br>
<p>CONTENTS
<br>
<p>1. <a href="#1">Introduction</a>
<br>2. <a href="#2">UNIX and Windows</a>
<br>3. <a href="#3">Error Codes</a>
<br>4. <a href="#4">Port Numbers</a>
<br>5. <a href="#5">Include Files</a>
<br> -<a href="#5.1"> 5.1 socket()</a>
<br> -<a href="#5.2">5.2 bind()</a>
<br> -<a href="#5.3">5.3 connect()</a>
<br> -<a href="#5.4">5.4 listen()</a>
<br> -<a href="#5.5">5.5 accept()</a>
<br> -<a href="#5.6">5.6 gethostname()</a>
<p>6. <a href="#6">Common Functions</a>
<br>7. <a href="#7"> Renamed Functions</a>
<br>8. <a href="#8">Blocking Routines</a>
<br>9. <a href="#9">Additional Functions</a>
<br>10. <a href="#10">Porting Code</a>
<br> -10.1 <a href="#10.1">DNS Program</a>
<br> -10.2 <a href="#10.2">Streaming Server</a>
<br> -10.3 Streaming Client
<p>11. <a href="#11">Planning</a>
<br>12. <a href="#12">Last Words</a>
<p> APPENDIXES
<p> A - <a href="#appxA">The Compiler</a>
<p>______________________
<br>1.0<a NAME="1"></a> INTRODUCTION
<br>
<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>
<p>
<hr SIZE=1 WIDTH="70%">
<br>
<p>2.0 <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>
<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>
<p>
<hr SIZE=1 WIDTH="70%">
<br>
<p>3.0 <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>
<p>_________________________
<p>4.0 <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>
<p>5.0 <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>
<p>5.1 <a NAME="5.1"></a>socket()
<p> [ #include <sys/types.h> ]
<br> [ #include <sys/socket.h> ]
<br>
<p>5.2 <a NAME="5.2"></a>bind()
<p> [ #include <sys/types.h> ]
<br> [ #include <sys/socket.h> ]
<br>
<p>5.3 <a NAME="5.3"></a>connect()
<p> [ #include <sys/types.h> ]
<br> [ #include <sys/socket.h> ]
<br>
<p>5.4 <a NAME="5.4"></a>listen()
<p> [ #include <sys/types.h> ]
<br> [ #include <sys/socket.h> ]
<br>
<p>5.5 <a NAME="5.5"></a>accept()
<p> [ #include <sys/socket.h> ]
<p>5.6 <a NAME="5.6"></a>gethostname()
<p> [ #include <netdb.h> ]
<br>
<br>
<p>You will need some general socket include files also in UNIX programming:
<p>#include <netinet/in.h>
<br>#include <arpa/inet.h>
<br>
<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 <sys/types.h>
<br>#include <sys/socket.h>
<br>#include <netdb.h>
<br>#include <netinet/in.h>
<br>#include <arpa/inet.h>
<br>
<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 <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>
<p>Here is a list of common functions.
<p>1. Socket()
<p>2. Bind()
<p>3. Listen()
<p>4. Connect()
<p>5. Accept()
<p>6. Send()
<p>7. Recv()
<p>8. Sendto()
<p>9. Recvfrom()
<p>10. Gethostname()
<br>
<p>
<hr SIZE=1 WIDTH="70%">
<br>
<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 + -