CP socket programming C/C ++ implementation (Windows Platform SDK)

Source: Internet
Author: User
Keyit. Focus ();}
C language TCP Program Hello world in. I used to use OO language all the time. Now I use C language to write and play games. I can't bend my mind. O (∩ _ ∩) o...
Server:

# Pragma comment (Lib, "ws2_32.lib ")

# Include <winsock2.h>

# Include <stdio. h>



Void main ()

{

// Version negotiation

Word wversionrequested;

Wsadata;

Int err;



Wversionrequested = makeword (0101); // 0 x

Err = wsastartup (wversionrequested, & wsadata );

If (Err! = 0)

{

Return;

}



If (lobyte (wsadata. wversion )! = 1 | hibyte (wsadata. wversion )! = 1)

// Wsadata. wversion! = 0x0101

{

Wsacleanup ();

Return;

}

// Create a socket

Socket socksvr = socket (af_inet, sock_stream, 0 );

// Create an IP address and port

Sockaddr_in addrsvr;

Addrsvr. sin_addr.s_un.s_addr = htonl (inaddr_any );

Addrsvr. sin_family = af_inet;

Addrsvr. sin_port = htons (6000 );

// Bind a port listener

BIND (socksvr, (sockaddr *) & addrsvr, sizeof (sockaddr ));

Listen (socksvr, 5 );



Sockaddr_in addrclient;

Int Len = sizeof (sockaddr );

While (true)

{

// Blocking method to obtain a client socket connection

Socket sockconn = accept (socksvr, (sockaddr *) & addrclient, & Len );

Char sendbuffer [128];

Sprintf (sendbuffer, "welcom % s! ", Inet_ntoa (addrclient. sin_addr ));

// Send data to the customer's socket

Send (sockconn, sendbuffer, strlen (sendbuffer) + 1, 0 );

Char recvbuffer [128];

// Accept data from the customer SOC

Recv (sockconn, recvbuffer, 0 );

Printf ("% s \ n", recvbuffer );

// Close the socket

Closesocket (sockconn );

}

Closesocket (socksvr );

// Release Winsock Resources

Wsacleanup ();

}

Client:

# Pragma comment (Lib, "ws2_32.lib ")

# Include <winsock2.h>

# Include <stdio. h>



Void main ()

{

// Version negotiation

Word wversionrequested;

Wsadata;

Int err;



Wversionrequested = makeword (0101); // 0 x

Err = wsastartup (wversionrequested, & wsadata );

If (Err! = 0)

{

Return;

}



If (lobyte (wsadata. wversion )! = 1 | hibyte (wsadata. wversion )! = 1)

// Wsadata. wversion! = 0x0101

{

Wsacleanup ();

Return;

}

// Create a socket connecting to the server

Socket sock = socket (af_inet, sock_stream, 0 );

// Create an address

Sockaddr_in hostaddr;

Hostaddr. sin_addr.s_un.s_addr = inet_addr ("127.0.0.1 ");

Hostaddr. sin_family = af_inet;

Hostaddr. sin_port = htons (6000 );

// Connect to the server

Connect (sock, (sockaddr *) & hostaddr, sizeof (sockaddr ));



Char revbuf [128];

// Obtain data from the server

Recv (sock, revbuf, 0 );

Printf ("% s \ n", revbuf );

// Send data to the server

Send (sock, "Hello host! ", 12, 0 );

Closesocket (sock );

}

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.