Writing Client/Server Program in C on UNIX Server
I am trying to write a basic C client/server program using sockets in Unix. I am logging on my school's Unix server from my home computer. I am logging on twice, once to开发者_如何学Python simulate the server and the other to simulate the client. Do I use AF_INET or AF_UNIX? Whose IP address should I use, the one from my home computer or my school? If I use my school's IP address, how do I find out what it is? How do I find out what port number I should use? Does the port number for my client and server have to be the same?
- Usually you'll want to use
AF_INET
— then you'll be able to communicate between more than one computer later. - Use
127.0.0.1
; that means "this computer". - Make up a port number and use it for both. Usually you'll want to pick something between 1024 and 65536, exclusive.
Since the programs are running on the schools computer you should use that IP address.
You can use the command /usr/sbin/ifconfig -a to find it (it's the inet address). However, if both logins are at the same physical computer (for example at my school that isn't always the case) you can just use 127.0.0.1.
You can use any port number you want, but choose a high (like four digits) since the lower ones are default for some services.
The port numbers for the client and server do not have to be the same, but it might be easier for you to remember if they are.
See here for a lot of examples. There is some stuff you have to understand:
- TCP, UDP and the difference between them
- What is a socket, types of sockets (stream, datagram, sequential packets)
- Socket API's - BSD, POSIX, WinSock (if you're planning to program for Windows)
精彩评论