Upload images through Linux Network Communication

Source: Internet
Author: User

Server. c

# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Include <sys/wait. h>
# Include <fcntl. h>
/* Local port to be listened on by the server */
# Define MYPORT 4000
/* How many connections without accept can be accepted simultaneously */
# Define BACKLOG 10
// Void sig_urg (int signo );
Int main ()
{
Int sock_fd, new_fd, fd;
/* Your own address information */
Struct sockaddr_in my_addr;
/* Connection provider address information */
Struct sockaddr_in their_addr;
Int sin_size;
Int n;
Char buff [2281] = {0 };
 
/* This is the error check we have always stressed. If the socket () call fails, the system returns */
If (sock_fd = socket (AF_INET, SOCK_STREAM, 0) =-1)
{
/* Output an error message and exit */
Perror ("socket ");
Exit (1 );
}
 
/* Host byte sequence */
My_addr.sin_family = AF_INET;
/* Network byte sequence, short integer */
My_addr.sin_port = htons (MYPORT );
/* Fill in the IP address of the machine running the program into s_addr */
My_addr.sin_addr.s_addr = INADDR_ANY; // Client ip Address
/* Clear the remaining space of the Structure */
Bzero (& (my_addr.sin_zero), 8 );
 
/* Here is the error check we have always stressed !! */
If (bind (sock_fd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr) =-1)
{
/* If bind () fails to be called, an error message is displayed. Exit */
Perror ("bind ");
Exit (1 );
}
/* Here is the error check we have always stressed !! */
If (listen (sock_fd, BACKLOG) =-1)
{
/* If the listen Call fails, an error message is displayed. Exit */
Perror ("listen ");
Exit (1 );
}

/// * Set the processing function sig_urg */
// Old_sig_urg_handle = signal (SIGURG, sig_urg );
/// * Change the owner of connfd */
// Fcntl (sock_fd, F_SETOWN, getpid ());
While (1)
{
/* Here is the main accept () loop */
Sin_size = sizeof (struct sockaddr_in );
/* Here is the error check we have always stressed !! */
If (new_fd = accept (sock_fd, (struct sockaddr *) & their_addr, & sin_size) =-1)
{
/* If an error occurs when you call accept (), an error message is displayed, entering the next loop */
Perror ("accept ");
Continue;
}
/* The server displays the connection information */
Printf ("server: got connection from % s \ n", inet_ntoa (their_addr.sin_addr ));
Printf ("buff: % s \ n", buff );
/* A sub-process will be established to communicate with the socket just created */
If (! Fork ())
{
/* Here is the sub-process */
While (1)
{

If (n = recv (new_fd, buff, (sizeof (buff)-1), 0) = 0)
{
Printf ("received EOF \ n ");
Break;
}
Buff [n] = 0;
Printf ("Recv % d bytes: % s \ n", n, buff );
Fd = open ("test.jpg", O_RDWR | O_CREAT, 0777 );
Write (fd, buff, sizeof (buff ));
Close (fd );
}
/* Close the socket connection represented by new_fd */
Close (new_fd );
}
}
Return 0;
}

/// * Wait for all sub-processes to exit */
// While (waitpid (-1, NULL, WNOHANG)> 0 );
/// * Restore the previous SIGURG processor */
// Signal (SIGURG, old_sig_urg_handle );
//}

// Void sig_urg (int signo)
//{
// Int n;
// Char buff [100];
// Printf ("SIGURG received \ n ");
// N = recv (new_fd, buff, sizeof (buff)-1, MSG_OOB );
// Buff [n] = 0;
// Printf ("recv % d OOB byte: % s \ n", n, buff );
//}

 

Client. c

# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <netdb. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Include <fcntl. h>
/* Port number monitored by the server program */
# Define PORT 4000
/* Maximum number of bytes that can be received at a time */
# Define MAXDATASIZE 2280 // if the number is smaller than the image size, the transmitted image is incomplete
Int main (int argc, char * argv [])
{
Int sockfd, numbytes, fd;
Char buf [MAXDATASIZE] = {0 };
Struct hostent * he; // server information
/* Host information of the connector client */
Struct sockaddr_in their_addr;
 
 
/* Check parameter information */
If (argc! = 2)
{
/* If no parameter exists, use the method and exit */
Fprintf (stderr, "usage: client hostname \ n ");
Exit (1 );
}
/* Obtain host information */
If (he = gethostbyname (argv [1]) = NULL)
{
/* If gethostbyname () is incorrect, the system displays the error message and exits */
Perror ("gethostbyname ");
Exit (1 );
}
If (sockfd = socket (AF_INET, SOCK_STREAM, 0) =-1)
{
/* If an error occurs during a socket () call, the error message is displayed and the interface exits */
Perror ("socket ");
Exit (1 );
}
 
// Enter the server information on the client
Bzero (& (their_addr.sin_zero), sizeof (their_addr ));
/* Host byte sequence */
Their_addr.sin_family = AF_INET;
/* Network byte sequence, short integer */
Their_addr.sin_port = htons (PORT );
Their_addr.sin_addr = * (struct in_addr *) he-> h_addr );
 
If (connect (sockfd, (struct sockaddr *) & their_addr, sizeof (struct sockaddr) =-1)
{
/* If a connection error occurs during connect (), an error message is displayed. Exit */
Perror ("Connect ");
Exit (1 );
}
FD = open ("8k.jpg", o_rdwr );
Read (FD, Buf, maxdatasize );
If (send (sockfd, Buf, maxdatasize, 0) =-1)
{
/* If an error occurs, an error message is displayed, and the new connection is closed to exit */
Perror ("send ");
Close (sockfd );
Exit (0 );
}
Printf ("Send 3 byte of normal data \ n ");
 
// Sleep (1 );
/*
If (send (sockfd, "4", 1, MSG_OOB) =-1)
{
Perror ("send ");
Close (sockfd );
Exit (0 );
}
Printf ("Send 1 byte of OOB data \ n ");
Sleep (1 );
If (send (sockfd, "56", 2, 0) =-1)
{
Perror ("send ");
Close (sockfd );
Exit (0 );
}
Printf ("Send 2 bytes of normal data \ n ");
Sleep (1 );
If (send (sockfd, "7", 1, MSG_OOB) =-1)
{
Perror ("send ");
Close (sockfd );
Exit (0 );
}
Printf ("Send 1 byte of OOB data \ n ");
Sleep (1 );
If (send (sockfd, "89", 2, MSG_OOB) =-1)
{
Perror ("send ");
Close (sockfd );
Exit (0 );
}
Printf ("Send 2 bytes of normal data \ n ");
//*/
Sleep (1 );
Close (fd );

Close (sockfd );
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.