In computing, socket programming, involves writing computer programs that communicate with other programs across a computer network. The program that initiate the communication is called a client, and the program waiting for the communication is the server. The client and server processes together form a distributed system.
Reference:
Socket Programming (2007), Last accessed April 5th, 2009 from
http://en.wikipedia.org/wiki/Socket_programming
2. Define some terms.
Socket: is the endpoint of a bidirectional communication flow across an Internet Protocol-based computer network, such as the Internet.
Socket Address: is the combination of an IP address (the location of the computer) and a port (which is mapped to the application program process) into a single identity.
Client: a computer chat program that initiates a communication with another computer.
Server: a computer client program that is being contacted by a client.
Reference:
Socket Programming (2007), Last accessed April 5th, 2009 from
http://en.wikipedia.org/wiki/Socket_programming
3. How is a socket bound to a port number and what is the role of the operating system on the server end?
The operating system forwards incoming IP packets to the corresponding application or service process by extracting the socket address information from the IP and transport protocol headers. Within the operating system and the application that created a socket, the socket is referred to by a unique integer number called socket number
Reference:
Internet Socket (2007), Last accessed April 5th, 2009 from
http://en.wikipedia.org/wiki/Internet_socket
4. Investigate a simple chat client/server system. Look at some program code and describe how it works with multiple users.
A chat system is divided into two program, a client program that needs to initiate the communication and a server program.
Here is a sample of a client chat program written in C:
#include
#include
#include
#include
int main() {
int serverfd;
struct sockaddr_in serveraddress;
char buf[200];
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(12205);
serveraddress.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&(serveraddress.sin_zero), 0, 8);
serverfd = socket(AF_INET, SOCK_STREAM, 0);
connect(serverfd, (struct sockaddr*)&serveraddress, sizeof(struct sockaddr));
while (fgets(buf, 200, stdin)) {
send(serverfd, buf, strlen(buf)+1, 0);
}
close(serverfd);
return 0;
}
And here is the server program:
#include
#include
#include
#include
int main() {
int serverfd, clientfd;
struct sockaddr_in myaddress;
struct sockaddr_in cliaddress;
char buf[200];
myaddress.sin_family = AF_INET;
myaddress.sin_port = htons(12205);
myaddress.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(myaddress.sin_zero), 0, 8);
serverfd = socket(AF_INET, SOCK_STREAM, 0);
bind(serverfd, (struct sockaddr *)&myaddress, sizeof(struct sockaddr));
listen(serverfd, 2);
while (1) {
int addrlen = sizeof(struct sockaddr);
int len;
clientfd = accept(serverfd, (struct sockaddr *)&cliaddress, &addrlen);
printf("[[connected to %s]]\n", inet_ntoa(cliaddress.sin_addr));
while (len = recv(clientfd, buf, 200, 0)) {
puts(buf);
}
printf("connection reset\n");
close(clientfd);
}
return 0;
}
Reference:
No comments:
Post a Comment