C client transmits XML to the server for parsing 2

Source: Internet
Author: User

///////////////////////////////////// //////////////////////////////////////// /// //
// multi_thread_server.c
///////////////////////// //////////////////////////////////////// //////////////
// The code of the multi-thread Concurrent Server /span>
# include // For sockaddr_in
# include // For socket
# include // For socket
# include // for printf
# include // For exit
# include // For bzero
# include
# include // For errno
# include // define close

# Include <string. h>
# Include <libxml/parser. h>
# Include <libxml/tree. h>

# Define hello_world_server_port 6666
# Define length_of_listen_queue 20
# Define buffer_size 1024
# Define thread_max 5

Void * getxml (char * argv );
Void * talk_to_client (void * Data)
{
// Void * data can be converted to any type of data

File * FP;

int new_server_socket = (INT) data;
int length = 0;
char buffer [buffer_size];
bzero (buffer, buffer_size );
char BF [] = "";
strcpy (buffer, "Hi. from the server ");
strcat (buffer," \ n "); // C language string connection
// send the string in the buffer to new_server_socket, actually, it is sent to the client
send (new_server_socket, buffer, buffer_size, 0);
bzero (buffer, buffer_size );
// receives information sent from the client to the buffer.
If (length = Recv (new_server_socket, Buf Fer, buffer_size, 0) <0)
{< br> printf ("server recieve data failed! \ N ");
exit (1);
}< br> printf (" \ nsocket num: % d \ t % s ", new_server_socket, buffer );

// Fp = fopen ("A. xml", "W"); // open the file
// Fwrite (buffer, sizeof (char), strlen (buffer), FP); // write a file
// Fclose (FP); // close

Getxml (buffer );

// send (new_server_socket, buffer, buffer_size, 0);
// close the connection to the client
close (new_server_socket );
pthread_exit (null);
}< br> void * getxml (char * argv)
{< br> xmldocptr Doc = NULL;
xmlnodeptr cur = NULL;
char * name = NULL;
char * value = NULL;

Xmlkeepblanksdefault (0 );

// If (argc <2)
//{
// Printf ("argc must be 2 or above \ n ");
// Return-1;
//}
// Doc = xmlparsefile (argv); // create a DOM tree
Doc = xmlrecoverdoc (argv); // create a DOM tree
If (Doc = NULL)
{

Printf ("loading XML file failed! \ N ");
Exit (1 );
}

Cur = xmldocgetrootelement (DOC); // get the root node
If (cur = NULL)
{

Printf ("empty file! \ N ");
Xmlfreedoc (DOC );
Exit (2 );
}
// Walk the tree
Cur = cur-> xmlchildrennode; // get sub Node
Int A = 0;
While (cur! = NULL)
{
Name = (char *) (cur-> name );
Value = (char *) xmlnodegetcontent (cur );
// Cout <"name is:" <name <", value is:" <value <Endl;
Printf ("\ nvalue is: % d \ t % s", name, value );
A + = atoi (value );
Xmlfree (value );
Cur = cur-> next;

}
Printf ("\ nresult is: % d \ t", );
Xmlfreedoc (DOC); // release resources used by the XML Parsing Library
Xmlcleanupparser ();
}

Int main (INT argc, char ** argv)
{
Int server_socket = 0;
Int new_server_socket = 0;
// Set a socket address structure server_addr, which represents the Internet address and port of the server.
Struct sockaddr_in server_addr;
Bzero (& server_addr, sizeof (server_addr); // set all content in a memory zone to 0
Server_addr.sin_family = af_inet;
Server_addr.sin_addr.s_addr = htonl (inaddr_any );
Server_addr.sin_port = htons (hello_world_server_port );
// Create a TCP socket for the Internet, and use server_socket to represent the server socket
Server_socket = socket (af_inet, sock_stream, 0 );
If (server_socket =-1)
{
Printf ("create socket failed! ");
Exit (1 );
}
// Link the socket and socket address Structures
If (BIND (server_socket, (struct sockaddr *) & server_addr, sizeof (server_addr )))
{
Printf ("Server Bind port: % d failed! ", Hello_world_server_port );
Exit (1 );
}
// Server_socket is used for listening
If (Listen (server_socket, length_of_listen_queue ))
{
Printf ("server listen failed! ");
Exit (1 );
}
Int I = 0;
While (1) // The server must always run
{
// Define the client's socket address structure client_addr
Struct sockaddr_in client_addr;
Socklen_t length = sizeof (client_addr );
// Accept a connection to the socket represented by server_socket
// If there is no connection request, wait for a connection request-this is a feature of the accept Function
// The accept function returns a new socket. This socket (new_server_socket) is used for communication with the connected client.
// New_server_socket represents a communication channel between the server and the client.
// The accept function fills in the connected client information in the client's socket address structure client_addr.

If (new_server_socket = accept (server_socket, (struct sockaddr *) & client_addr, & length) =-1)
{
Printf ("Server Accept failed! \ N ");
Break;
}
Pthread_t child_thread;
Pthread_attr_t child_thread_attr;
Pthread_attr_init (& child_thread_attr );
Pthread_attr_setdetachstate (& child_thread_attr, pthread_create_detached );
If (pthread_create (& child_thread, & child_thread_attr, talk_to_client, (void *) new_server_socket) <0)
{
Printf ("pthread_create failed: % s \ n", strerror (errno ));
}
// Getxml ();

}
// Disable the socket used for listening
Close (server_socket );
Return 0;

}

Related Article

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.