4)
How to use fgets( ) to read on a file/socket descriptor?
6)
We often use fgets( ) and read( ) to read something. What
is difference between two functions?
7)
What are the well-known port numbers?
8)
Which Unix servers can I log on besides
gigastar.eng.wayne.edu and yellow.eng.wayne.edu?
10) How to
use vi editor commands?
11) How to indicate
the end of transfer for socket communication?
-----------------------------------------------------------------------------
1) I want to
know the manual about the read system call,
but %man read always responds with the
manual about the user command read.
Answer:
When the same name is used for user command, system call, or even C standard
function, please use option –s to
distinguish them. %man –s 2 read will give
you the manual for the read system call, not
the user command read. %man –s 3c
mkfifo will give you the manual for the standard C function mkfifo, not the user command mkfifo.
2) May I ask
where the functions err_sys() and sys_err_str() refer to? When I compile my program, it
always complains "undefined reference to err_sys()"
and "sys_err_str()". Do I need to
create the functions by myself?
Answer: A
simple way is to replace such functions by printf()
or perror().
3) What is the
difference between open(),read(), write(), close(), etc. and fopen(), fread(),
fwrite(), fclose(), etc.?
Answer: The
first group are the Unix system calls for I/O which are direct entry points
into the kernel. They can be used not only for files but also for IPC channels
(such as PIPE, FIFO) and Sockets. The second group are the standard I/O library
functions, providing a higher level interface between a process and the kernel
with features such as buffering, line-by-line input, formatted output, and the
like. The second group will be finally implemented with the first group. With network programming, however, there are times when the
Unix system calls must be used.
The
next question may help to better understand the difference between file
descriptor and file pointer.
4) How to use fgets( ) to read on a file/socket descriptor?
Answer: There
are several ways:
|
Way
1 |
Way
2 |
Way
3 |
|
int
fd; close(0); fd=socket(…); fgets(…,stdin); |
int
fd; fd=socket(…); dup2(fd,
0); fgets(…,stdin); |
int
fd; FILE
*fp; fd=socket(…); fp=fdopen(fd); fgets(…,fp); |
Beside,
we can also do as such:
struct FILE {int fd; int cnt;
char *bar; int flag;} *fp;
int fd=socket(…);
fp->fd=fd;
fp->cnt=0;
fp->bar=NULL;
fp->flag=0;
fgets(…,fp);
5) Could you
tell me what the option "-lsocket -lnsl"
is used for? How can I find this kind of options with command man? ( I
tried man gcc, but cannot find them)
Answer: "-lsocket -lnsl" means that some
additional libraries (except standard library) are needed when linking. In
this case, libsocket.a and libnsl.a in /usr/lib/
are needed. You can try "%man gcc" and
then jump to /LINKER for more details about the option -l.
You can use "%man libsocket" and "%man libnsl" to know the contents in such libraries.
6) We often
use fgets( ) and read( ) to read something. What is difference between two
functions?
Answer: Their differences are summarized in the following table. If you are still not clear, you might need to write some programs to get hands-on experience.
|
|
fgets(line, MAXLINE, fp ) |
read(fd, buf, MAXBUF ) |
|
purpose |
Read a line |
Read a block |
|
source |
C Standard
function |
Unix system
call |
|
object |
File pointer |
File descriptor |
|
Return value |
>0:sucessfule;
-1 on error |
The number of
bytes read; -1 on error |
|
Limit of bytes |
At most MAXLINE-1
bytes are read, leaving the last byte for termination ‘\0’ |
At most BUFSIZE
bytes are read. |
|
\n |
Stop when
meeting with ‘\n’ |
Don’t care. But
if fd=stdin, read() will stop when meeting with ‘\n’ and get ‘\n’ as
the last character. |
|
EOF |
Both read EOF
separately. fgets( ) return NULL; read( ) returns 0. |
|
|
String ended
with ‘\0’ in buffer |
Yes |
No |
7) What are
the well-known port numbers?
Answer: The port numbers for well-known network applications are listed in the following table.
|
Port
number |
Network
Applications |
|
20 |
ftp data |
|
21 |
ftp open
server |
|
23 |
telnet |
|
25 |
smtp |
|
31 |
master
paradise.80 |
|
53 |
dns、bonk
(dos exploit) |
|
79 |
finger |
|
80 |
http |
|
110 |
pop3 |
|
113 |
auther
idnet |
|
119 |
nntp |
|
121 |
bo
jammerkillah |
|
137 |
netbios-ns |
|
138 |
netbios-dgn |
|
139 |
netbios-ssn |
|
143 |
imap |
|
161 |
snmp |
|
162 |
snmp-trap |
8) Which
Unix servers can I log on besides gigastar.eng.wayne.edu and
yellow.eng.wayne.edu?
Answer: you have plenty of choices. See the following list or http://www.eng.wayne.edu/coe/main.cfm?location=385
.
|
IP Address |
Server Name |
IP Address |
Server Name |
|
141.217.200.6 |
morocco |
141.217.200.46 |
norway |
|
141.217.200.7 |
lethoso |
141.217.200.47 |
sweden |
|
141.217.200.8 |
egypt |
141.217.200.48 |
finland |
|
141.217.200.9 |
chad |
141.217.200.49 |
netherlands |
|
141.217.200.10 |
sudan |
141.217.200.50 |
denmark |
|
141.217.200.11 |
malawi |
141.217.200.51 |
estoria |
|
141.217.200.12 |
mali |
141.217.200.52 |
latvia |
|
141.217.200.13 |
zambia |
141.217.200.53 |
lithuania |
|
141.217.200.14 |
kenya |
141.217.200.54 |
ireland |
|
141.217.200.15 |
zimbabwe |
141.217.200.55 |
poland |
|
141.217.200.16 |
migeria |
141.217.200.56 |
ukraine |
|
141.217.200.17 |
ghana |
141.217.200.57 |
romania |
|
141.217.200.18 |
uganda |
141.217.200.58 |
bulgaria |
|
141.217.200.19 |
zaire |
141.217.200.59 |
greece |
|
141.217.200.20 |
somalia |
141.217.200.60 |
macedonia |
|
141.217.200.21 |
gabon |
141.217.200.61 |
yugoslavia |
|
141.217.200.22 |
congo |
141.217.200.62 |
bosnia |
|
141.217.200.23 |
angola |
141.217.200.63 |
croatia |
|
141.217.200.24 |
togo |
141.217.200.64 |
slovenia |
|
141.217.200.25 |
namibia |
141.217.200.65 |
austria |
|
141.217.200.26 |
rwanda |
141.217.200.66 |
switzerland |
|
141.217.200.27 |
comoros |
141.217.200.67 |
france |
|
141.217.200.28 |
entrea |
141.217.200.68 |
spain |
|
141.217.200.29 |
liberia |
141.217.200.69 |
portugal |
|
141.217.200.30 |
senegal |
141.217.200.70 |
belgium |
|
141.217.200.31 |
burundi |
141.217.200.71 |
germany |
|
141.217.200.32 |
benin |
141.217.200.72 |
hungary |
|
141.217.200.33 |
mayotte |
141.217.200.73 |
slovakia |
|
141.217.200.34 |
cameroon |
141.217.200.74 |
iceland |
|
141.217.200.35 |
ethiopia |
141.217.200.75 |
italy |
|
141.217.200.36 |
madagascar |
|
|
Answer: “Mega can mean either 220 or 106.
What is worse; in networking we typically use both definitions. Here is the
story. Network bandwidth, which is often specified in terms of Mbps, is
typically governed by the speed of the clock that paces the transmission of the
bits. A clock that is running at 10 MHz is used to transmit bits at 10 Mbps.
Because the mega in MHz means 106, Mbps is usually also
defined as 106 bits per second. On the other hand, when we talk
about a message that we want to transmit, we often give its size in megabytes.
Because messages are stored in the computer memory, and memory is typically
measured in powers of 2, the M in MB is usually taken to mean 220.” ------L.L. Peterson and B.S. Davie
10) How to use vi editor commands?
Answer:
1. Moving Cursor Around
|
k |
up |
|
h l |
left right |
|
j |
down |
|
$ |
Move to end of line |
|
0 |
Move to begin of line |
|
G |
Move to end of file |
|
1G |
Move to head of file |
|
w |
Forward a word stopping at punctuation |
|
b |
Backward a word stopping at punctuation |
|
W |
Forward a word ignoring punctuation |
|
b |
Backward a word ignoring punctuation |
|
e |
To end of word or punctuation |
|
E |
To end of word |
|
CTRL-b |
One page up |
|
CTRL-f |
One page down |
|
CTRL-g |
Show current line number |
|
CTRL-l |
Redraw the display |
2. Editing Text
|
xp |
Transpose two characters |
|
. |
Repeat last text change command at
current cursor position |
|
/myword |
Forward to any existing occurrence of
myword |
|
?myword |
Backwards to any existing occurrence of
myword |
|
n |
Next occurrence of myword in the search
direction |
|
N |
Next occurrence of myword in the opposite
search direction |
|
I |
Insert text at beginning of the line |
|
A |
Insert text after end of the line. |
|
dw |
Delete one word |
|
D |
Delete from cursor to the end of the line |
|
d$ |
Delete to end of line |
|
n~ |
change the case of n letters and
advance the cursor n spaces |
|
r |
replace a character |
|
R |
Replaces everything until press
<ESC> |
|
cw |
Changes a word to everything typed
between cw and <ESC> |
|
cc |
Overwrites the current line with whatever
text you insert |
|
u |
Undoes the last step you did |
|
U |
Restores current line |
3. Set Command
|
:set nu |
Set number |
|
:set nonu |
Set number off |
|
:set list |
Show tabs as ^l and returns as $ |
|
:set nolist |
Set list off |
|
:set nows |
Stop searches wrapping round to other end
of file |
|
:set ws |
Set searches wrapping round to other end
of file |
|
:f |
Show current FILE name and cursor line
number |
4. Copy and Paste
|
yy |
Yanks one line into buffer without
deleting |
|
"ayy |
yank into the buffer named a |
|
P |
put the general buffer
back before the current line |
|
"aP |
put from buffer a before current line |
|
p |
put the general buffer
back after the current line |
|
"ap |
put from buffer a after the
current line |
5. Substitution
|
:%s/oldword/newword/g |
Change all instance of old
word to newword |
11) How to indicate the end of
transfer or one record for socket communication?
Answer: Method
1: close the connection so that read( ) at the other end returns “NULL”. This is only useful for one-time transfer, e.g., HTTP. Method 2: use a special string pattern but theorectically any special string pattern might
appear in the file or transmission. For example, FTP and SMTP use the 2-byte sequence of an ASCII carriage return(\r) followed by an ASCII linefeed(\n). Method 3: Send “EOT” or “EOF” so that read(
) return NULL. But how to send EOT or EOF? I haven’t tried it. Method 4: Use stat(
) or fstat( ) to get the file size and then tell it to the receiver as a
counter. The receiver will stop the receiving when the counter decreases to
zero. RPC and DNS use this method. Method 5: Use ping-pong protocol. Each packet is appended with a header
(e.g., 1 or 0) which indicates if
it is the last packet. Each message cannot be longer than MAXLENGTH of read( ).