ورود

View Full Version : سوال: درخواست تحلیل برنامه ای در لینوکس



csdeveloper
شنبه 17 تیر 1391, 23:02 عصر
سلام دوستان
لطفا اگر کسی میتواند بنده رو در تحلیل این برنامه کمک کند.
با تشکر


An Example Using Local Namespace Sockets

We illustrate sockets with two programs.The server program, in Listing 5.10, creates a

local namespace socket and listens for connections on it.When it receives a connection,

it reads text messages from the connection and prints them until the connection

closes. If one of these messages is “quit,” the server program removes the socket and

ends.The socket-server program takes the path to the socket as its command-line

argument.

(socket-server.c) Local Namespace Socket Server

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <sys/un.h>

#include <unistd.h>

/* Read text from the socket and print it out. Continue until the

socket closes. Return nonzero if the client sent a “quit”

message, zero otherwise. */

int server (int client_socket)

{

while (1) {

int length;

char* text;

/* First, read the length of the text message from the socket. If

read returns zero, the client closed the connection. */

if (read (client_socket, &length, sizeof (length)) == 0)

return 0;

/* Allocate a buffer to hold the text. */

text = (char*) malloc (length);

/* Read the text itself, and print it. */

read (client_socket, text, length);

printf (“%s\n”, text);

/* Free the buffer. */

free (text);

/* If the client sent the message “quit,” we’re all done. */

if (!strcmp (text, “quit”))

return 1;

}

}

int main (int argc, char* const argv[])

{

const char* const socket_name = argv[1];

int socket_fd;

struct sockaddr_un name;

int client_sent_quit_message;

/* Create the socket. */

socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);

/* Indicate that this is a server. */

name.sun_family = AF_LOCAL;

strcpy (name.sun_path, socket_name);

bind (socket_fd, &name, SUN_LEN (&name));

/* Listen for connections. */

listen (socket_fd, 5);

/* Repeatedly accept connections, spinning off one server() to deal

with each client. Continue until a client sends a “quit” message. */

do {

struct sockaddr_un client_name;

socklen_t client_name_len;

int client_socket_fd;

/* Accept a connection. */

client_socket_fd = accept (socket_fd, &client_name, &client_name_len);

/* Handle the connection. */

client_sent_quit_message = server (client_socket_fd);

/* Close our end of the connection. */

close (client_socket_fd);

}

while (!client_sent_quit_message);

/* Remove the socket file. */

close (socket_fd);

unlink (socket_name);

return 0;

}

The client program, in Listing 5.11, connects to a local namespace socket and sends

a message.The name path to the socket and the message are specified on the


command line.