Linux socket programming example

Source: Internet
Author: User

========================================================== ========

======= Server. c ========================================

# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <string. h>
# Include <fcntl. h>
# Include <dirent. h>
# Include <sys/types. h>/* See NOTES */
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <arpa/inet. h>
# Include <errno. h>


# Define N 128


Typedef struct sockaddr SA;




Void ProcessList (int connfd)
{
Char buf [N];
DIR * mydir;
Struct dirent * myitem;


Mydir = opendir (".");
While (myitem = readdir (mydir ))! = NULL)
{
If (strcmp (myitem-> d_name, ".") = 0) | (strcmp (myitem-> d_name, "..") = 0) continue;
Strcpy (buf, myitem-> d_name );
Send (connfd, buf, N, 0 );
}
Closedir (mydir );


Return;
}




Void ProcessGet (int connfd, char buf [])
{
Int fd, nbyte;


If (fd = open (buf + 1, O_RDONLY) <0)
{
Fprintf (stderr, "fail to open % s: % s \ n", buf + 1, strerror (errno ));
Buf [0] = 'n ';
Send (connfd, buf, N, 0 );
Return;
}

Buf [0] = 'y ';
Send (connfd, buf, N, 0 );
While (nbyte = read (fd, buf, N)> 0)
{
Send (connfd, buf, nbyte, 0 );
}
Close (fd );


Return;
}




Void ProcessPut (int connfd, char buf [])
{
Int fd, nbyte;


If (fd = open (buf + 1, O_WRONLY | O_CREAT | O_TRUNC, 0666) <0)
{
Printf ("fail to create % s on server \ n", buf + 1 );
Return;
}


While (nbyte = recv (connfd, buf, N, 0)> 0)
{
Write (fd, buf, nbyte );
}
Close (fd );


Return;
}




Int main (int argc, char * argv [])
{
Int listenfd, connfd;
Char buf [N];
Struct sockaddr_in server_addr;


// XXX: step 1 int socket (int domain, int type, int protocol );
If (listenfd = socket (PF_INET, SOCK_STREAM, 0) <0)
{
Fprintf (stderr, "fail to socket: % s \ n", strerror (errno ));
Exit (-1 );
}
# Ifdef _ DEBUG _
Printf ("socket is % d \ n", listenfd );
# Endif


// XXX: step 2 int bind (int sockfd, const struct sockaddr * serv_addr, socklen_t addrlen );
Memset (& server_addr, 0, sizeof (server_addr ));
Server_addr.sin_family = PF_INET;
Server_addr.sin_port = htons (7008 );
// Server_addr.sin_addr.s_addr = htonl (INADDR_ANY );
Server_addr.sin_addr.s_addr = INADDR_ANY;
If (bind (listenfd, (SA *) & server_addr, sizeof (server_addr) <0)
{
Perror ("fail to bind ");
Exit (-1 );
}


Listen (listenfd, 5 );

While (1)
{
If (connfd = accept (listenfd, NULL, NULL) <0)
{
Perror ("fail to accept ");
Break;
}
Recv (connfd, buf, N, 0 );
Switch (buf [0])
{
Case 'l ':
ProcessList (connfd );
Break;
Case 'G ':
ProcessGet (connfd, buf );
Break;
Case 'p ':
ProcessPut (connfd, buf );
Break;
}
Close (connfd );
}


Return 0;
}



========================================================== ==================================

========= Client. c ======================================================== ==========

# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <string. h>
# Include <fcntl. h>
# Include <sys/types. h>/* See NOTES */
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <arpa/inet. h>
# Include <errno. h>




// # Include <string. h>
// # Include <netdb. h>


# Define N 128


Typedef struct sockaddr SA;




Void PrintHelp ()
{
Printf ("help: display help info \ n ");
Printf ("list: get file list of server \ n ");
Printf ("get: get <file> \ n ");
Printf ("put: put <file> \ n ");
Printf ("quit: quit the client \ n ");


Return;
}




Void ProcessList (struct sockaddr_in server_addr)
{
Int sockfd, nbyte;
Char buf [N];


If (sockfd = socket (PF_INET, SOCK_STREAM, 0) <0)
{
Printf ("fail to list \ n ");
Return;
}


If (connect (sockfd, (SA *) & server_addr, sizeof (server_addr) <0)
{
Printf ("fail to connect server \ n ");
Goto ERROR_1;
}


Strcpy (buf, "L ");
Send (sockfd, buf, N, 0 );
While (nbyte = recv (sockfd, buf, N, 0 ))! = 0)
{
Printf ("% s \ n", buf );
}


ERROR_1:
Close (sockfd );

Return;
}




Void ProcessGet (struct sockaddr_in server_addr, char command [])
{
Int sockfd, nbyte, fd;
Char buf [N];


If (sockfd = socket (PF_INET, SOCK_STREAM, 0) <0)
{
Printf ("fail to get \ n ");
Return;
}


If (connect (sockfd, (SA *) & server_addr, sizeof (server_addr) <0)
{
Printf ("fail to connect server \ n ");
Goto ERROR_2;

}


Sprintf (buf, "G % s", command + 4 );
Send (sockfd, buf, N, 0 );
Recv (sockfd, buf, N, 0 );
If (buf [0] = 'n') // no such file
{
Printf ("No such file on server \ n ");
Goto ERROR_2;
}


If (fd = open (command + 4, O_WRONLY | O_CREAT | O_TRUNC, 0666) <0)
{
Printf ("fail to create local file % s \ n", command + 4 );
Goto ERROR_2;
}
 
While (nbyte = recv (sockfd, buf, N, 0)> 0)
{
Write (fd, buf, nbyte );
}
Close (fd );


ERROR_2:
Close (sockfd );


Return;
}




Void ProcessPut (struct sockaddr_in server_addr, char command [])
{
Int sockfd, fd, nbyte;
Char buf [N];


If (sockfd = socket (PF_INET, SOCK_STREAM, 0) <0)
{
Printf ("fail to get \ n ");
Return;
}


If (connect (sockfd, (SA *) & server_addr, sizeof (server_addr) <0)
{
Printf ("fail to connect server \ n ");
Goto ERROR_3;

}


If (fd = open (command + 4, O_RDONLY) <0)
{
Printf ("fail to open % s \ n", command + 4 );
Goto ERROR_3;
}
Sprintf (buf, "P % s", command + 4 );
Send (sockfd, buf, N, 0 );


While (nbyte = read (fd, buf, N)> 0)
{
Send (sockfd, buf, nbyte, 0 );
}
Close (fd );


ERROR_3:
Close (sockfd );


Return;
}




Int main (int argc, char * argv [])
{
Int sockfd, fd, nbyte;
Char command [32];
Struct sockaddr_in server_addr;

// Struct hostent * he;


If (argc <3)
{
Printf ("Usage: % s <server_ip >:< port> \ n", argv [0]);
Exit (-1 );
}

// XXX: step 1 int socket (int domain, int type, int protocol );
If (sockfd = socket (PF_INET, SOCK_STREAM, 0) <0)
{
Fprintf (stderr, "fail to socket: % s \ n", strerror (errno ));
Exit (-1 );
}
# Ifdef _ DEBUG _
Printf ("socket is % d \ n", sockfd );
# Endif
/* Obtain host information */
// If (he = gethostbyname (argv [1]) = NULL)
//{
/* If gethostbyname () is incorrect, the system displays the error message and exits */
// Herror ("gethostbyname ");
// Exit (1 );
//}
// XXX: step 2 int connect (int sockfd, const struct sockaddr * serv_addr, socklen_t addrlen );
Memset (& server_addr, 0, sizeof (server_addr ));
Server_addr.sin_family = PF_INET;
Server_addr.sin_port = htons (atoi (argv [2]);
// Server_addr.sin_port = htons (4000 );
Server_addr.sin_addr.s_addr = inet_addr (argv [1]);
// Server_addr.sin_addr = * (struct in_addr *) he-> h_addr );
// Server_addr.sin_addr.s_addr = htonl (INADDR_ANY );



While (1)
{
Printf ("<client> ");
Fgets (command, 32, stdin );
Command [strlen (command)-1] = '\ 0'; // overwrite the' \ N'
If (strcmp (command, "help") = 0)
{
PrintHelp ();
}
Else if (strcmp (command, "list") = 0)
{
ProcessList (server_addr );
}
Else if (strncmp (command, "get", 4) = 0)
{
ProcessGet (server_addr, command );
}
Else if (strncmp (command, "put", 4) = 0)
{
ProcessPut (server_addr, command );
}
Else if (strcmp (command, "quit") = 0)
{
Printf ("Bye \ n ");
Break;
}
Else
{
Printf ("wrong command, 'help' for command list \ n ");
}
}


Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.