C-written server and client

Source: Internet
Author: User

Server:

// Server. cpp: defines the entry point for the console application.

# Include "stdafx. H"
# Include <winsock2.h>
# Include <stdio. h>
# Include <windows. h>
// Tell the connector to connect to the ws2.32 Library
# Pragma comment (Lib, "ws2_32.lib ")
Int main (INT argc, char * argv [])
{

// Initialize ws2_32.dll
Wsadata;
Word wsockversion = makeword (2, 0 );
: Wsastartup (wsockversion, & wsadata );

// Create a socket
Socket S =: socket (af_inet, sock_stream, ipproto_tcp );
If (S = invalid_socket)
{
Printf ("failed socket./N ");
: Wsacleanup ();
Return 0;
}

// Fill in the sockaddr_in Structure
Sockaddr_in sin;
Sin. sin_family = af_inet;
Sin. sin_port = htons (8888 );
Sin. sin_addr.s_un.s_addr = inaddr_any;

// Bind
If (: BIND (S, (lpsockaddr) & sin, sizeof (SIN) = socket_error)
{
Printf ("failed bind./N ");
: Wsacleanup ();
Return 0;
}

// Listen
If (: Listen (S, 2) = socket_error)
{
Printf ("failed listen./N ");
Return 0;
}

// Cyclically accept customer connection requests
Sockaddr_in remoteaddr;
Int naddlen = sizeof (remoteaddr );
Socket Client;
Char sztext [] = "10 server demo! /R/N ";
While (true)
{
// Receive new connections
Client =: accept (S, (sockaddr *) & remoteaddr, & naddlen );
If (client = invalid_socket)
{
Printf ("failed accept ()./N ");
Continue;
}
Printf ("received a connection: % S/R/N", inet_ntoa (remoteaddr. sin_addr ));
// Send data to the client
: Send (client, sztext, strlen (sztext), 0 );
// Close the connection with the client
: Closesocket (client );
}
// Close
: Closesocket (s );
// Release the ws2_32 Library
: Wsacleanup ();
Return 0;
}

Client:

// Client. cpp: defines the entry point for the console application.

# Include "stdafx. H"
# Include <winsock2.h>
# Include <stdio. h>
# Include <windows. h>
# Pragma comment (Lib, "ws2_32.lib ")
Int main (INT argc, char * argv [])
{
Wsadata;
Word wsockversion = makeword (2, 0 );
: Wsastartup (wsockversion, & wsadata );
// Create a socket
Socket S =: socket (af_inet, sock_stream, ipproto_tcp );
If (S = invalid_socket)
{
Printf ("failed socket ()./N ");
: Wsacleanup ();
Return 0;
}

//
Sockaddr_in servaddr;
Servaddr. sin_family = af_inet;
Servaddr. sin_port = htons (8888 );
Servaddr. sin_addr.s_un.s_addr = inet_addr ("127.0.0.1 ");
If (: connect (S, (sockaddr *) & servaddr, sizeof (servaddr) =-1)
{
Printf ("failed connect ()./N ");
: Wsacleanup ();
Return 0;
}
Char buff [256];
Int nrecv =: Recv (S, buff, 256, 0 );
If (nrecv> 0)
{

Printf ("received data: % s", buff );
}
: Closesocket (s); // close
: Wsacleanup ();
Return 0;
}

Use vc6. The client is successfully connected to the server. The server records the IP address of the client.

The above C Code uses something else, so if it is saved. C. It cannot be compiled with VC. If you want to save it. C, compiled again, needs to be changed to the following:

Server:
# Include <winsock2.h>
# Include <stdio. h>
# Include <windows. h>
# Pragma comment (Lib, "ws2_32.lib ")

Int main (INT argc, char * argv [])
{
Wsadata;
Socket S;
Struct sockaddr_in servaddr;
Char buff [256];
Int nrecv;

Word wsockversion = makeword (2, 0 );
Wsastartup (wsockversion, & wsadata );
// Create a socket
S = socket (af_inet, sock_stream, ipproto_tcp );
If (S = invalid_socket)
{
Printf ("failed socket ()./N ");
Wsacleanup ();
Return 0;
}

//
Servaddr. sin_family = af_inet;
Servaddr. sin_port = htons (8888 );
Servaddr. sin_addr.s_un.s_addr = inet_addr ("127.0.0.1 ");
If (connect (S, (struct sockaddr *) & servaddr, sizeof (servaddr) =-1)
{
Printf ("failed connect ()./N ");
Wsacleanup ();
Return 0;
}

Nrecv = Recv (S, buff, 256, 0 );
If (nrecv> 0)
{
Buff [nrecv] = '/0 ';
Printf ("received data: % s", buff );
}
Closesocket (s); // close
Wsacleanup ();
Return 0;
}

Client:

# Include <winsock2.h>
# Include <stdio. h>
# Include <windows. h>
# Pragma comment (Lib, "ws2_32.lib ")

Int main (INT argc, char * argv [])
{
Wsadata;
Socket S;
Struct sockaddr_in servaddr;
Char buff [256];
Int nrecv;

Word wsockversion = makeword (2, 0 );
Wsastartup (wsockversion, & wsadata );
// Create a socket
S = socket (af_inet, sock_stream, ipproto_tcp );
If (S = invalid_socket)
{
Printf ("failed socket ()./N ");
Wsacleanup ();
Return 0;
}

//
Servaddr. sin_family = af_inet;
Servaddr. sin_port = htons (8888 );
Servaddr. sin_addr.s_un.s_addr = inet_addr ("127.0.0.1 ");
If (connect (S, (struct sockaddr *) & servaddr, sizeof (servaddr) =-1)
{
Printf ("failed connect ()./N ");
Wsacleanup ();
Return 0;
}

Nrecv = Recv (S, buff, 256, 0 );
If (nrecv> 0)
{
Buff [nrecv] = '/0 ';
Printf ("received data: % s", buff );
}
Closesocket (s); // close
Wsacleanup ();
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.