CSS 432
FAQ on Program 1: Sockets
Q1: Should we use C or C++?
A: Use C++.
Q2: May I use the code in the lecture slides from CSS432 and/or
CSS434?
A: Yes, you can
Q3: Should I use the "flush" system call?
A: No.
For data transfer, you should just use write, writev and read system calls.
Q4: What header files should I inclulde for each system call?
A: Use man.
If you would like to know the usage and the header files required
for the "write" system call, type
man 2 write
where 2 means the section 2. In general, most system calls are
described the section 2. The "man" page displays:
SYNOPSIS
#include
ssize_t write(int fd, const void *buf, size_t count);
...
So, what you need is to include the "unistd.h" file.
Q5. How can a server know the size of message it receives from the
client?
A. You may hard-code it in the server.
Q6. I got a "Broken pipe" error.
A. Your client and server program didn't agree at the number of
messages exchange and the message size, and thus one of them exited
earlier than the other.
If either cleint or server exits and its counter part still tries to send
a message, the socket (=pipe) has been already gone and you'll get a
broekn error.
Q7. When I tried to compile the following program, I got a bunch of errors.
int main( void ) {
write( 5, "Hi there!", 9 );
return 0;
}
A. Include header files necessary to execute the "write" system call and
open a file descriptor first.
Check the answer for Q4. Thereafter, you have to receive a file descriptor
explicitly from OS with open. In the above code, you are using the file
descriptor 5 without receiving this descriptor from OS. First open a file
or a socket.
Note that you can use the file descriptors 0, 1, and 2 without calling
open( ). They are automatically opened and correspond to stdin,
stdout, and stderr respectively.
Q8. The compiler complains about my usage of accept.
int len;
newSd = accept(sd, (strcute sockaddr *)&client, &len);
A. Use "socklen_t*" instead of "int*"
socklen_t len;
newSd = accept(sd, (strcute sockaddr *)&client, &len);
Q9. Do you want us to actually fill the message exchange between
a client and a server?
A. No, you don't have to fill it out.
Q10. What is the main difference between signal( ) and sigaction( )?
A.The signal( ) function resents the signal actioin back to
SIG_DFL (default) from SIGIO, whereas sigaction( ) doesn't
This means that you need to set up your signal handler every time you
return from it. However, in our program 1, our server needs to catch
only the very first signal from the server socket. Therefore, we
should use signal( ) rather than sigaction( ).
Q11. I got an error when using Socket.h and Socket.cpp
I tried to use the Socket.cpp and Socket.h on the professor has implemented.
However, when I try to compile those have this error.
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
A. You need the main( ) function
int main( int argc, char *argv[] ) {
}
Q12. My client program can't establish a connection to my server
I have passed a correct sequence of arguments like:
argv[0]: a server IP port
argv[1]: repetition
argv[2]: nbufs
argv[3]: serverIp
argv[4]: test type
But, it seems like my server can't accept a TCP connection request.
A. argv[0] is your client program's name. The actual arguments will start from argv[1]
In C++,
argv[0]: the client program name (such as a.out)
argv[1]: a server IP port
argv[2]: repetition
argv[3]: nbufs
argv[4]: serverIp
argv[5]: test type
Q13. What does 5 in connect( sd, 5 ) mean?
A.5 is the length of the connection request queue. This means
that, OS can hold up to five conneciton requests that have not yet
been accepted by your server program.
Q14. How do you receive from the server an integer acknowledgment
that shows how many times the server called read( )?
A. Use the following code.
int count = 0;
read( sd, (char *)&count, sizeof( count ) );