TCP/UDP socket programming C/C ++ implementation (Windows Platform SDK)

Source: Internet
Author: User
Tags htons

TCP socket programming C/C ++ implementation (Windows Platform SDK)
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 );
}
From: http://blog.csdn.net/nyzhl/archive/2008/07/04/2612815.aspx
UDP socket programming C/C ++ implementation (Windows Platform SDK)
Server:
-------------------------------------------------------
# Pragma comment (Lib, "ws2_32.lib ")
# Include <winsock2.h>
# Include <stdio. h>
Void main ()
{
// Version negotiation
Word wversionrequested;
Wsadata;
Int err;
Wversionrequested = makeword (1, 1 );
Err = wsastartup (wversionrequested, & wsadata );
If (Err! = 0 ){
/* Tell the user that we cocould not find a usable */
/* Winsock DLL .*/
Return;
}
/* Confirm that the Winsock DLL supports 2. 2 .*/
/* Note that if the DLL supports Versions greater */
/* Than 2.2 In addition to 2.2, it will still return */
/* 2.2 in wversion since that is the version we */
/* Requested .*/
If (lobyte (wsadata. wversion )! = 1 |
Hibyte (wsadata. wversion )! = 1 ){
/* Tell the user that we cocould not find a usable */
/* Winsock DLL .*/
Wsacleanup ();
Return;
}
/* The Winsock DLL is acceptable. Proceed .*/
// Create a datagram socket
Socket SVR = socket (af_inet, sock_dgram, 0 );
// Create a local address
Sockaddr_in ADDR;
ADDR. sin_family = af_inet;
ADDR. sin_port = htons (6000 );
ADDR. sin_addr.s_un.s_addr = htonl (inaddr_any );
Int Len = sizeof (sockaddr );
BIND (SVR, (sockaddr *) & ADDR, Len );
// Create a client address object
Sockaddr_in addrclient;
Char recvbuf [128];
Char sendbuf [128];
Char tempbuf [256];
While (true)
{
// Receive data
Recvfrom (SVR, recvbuf, 0, (sockaddr *) & addrclient, & Len );
Char * ipclient = inet_ntoa (addrclient. sin_addr );
Sprintf (tempbuf, "% s said: % s/n", ipclient, recvbuf );
Printf ("% s", tempbuf );
Gets (sendbuf );
// Send data
Sendto (SVR, sendbuf, strlen (sendbuf) + 1,0, (sockaddr *) & addrclient, Len );
}
Closesocket (SVR );
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 (1, 1 );
Err = wsastartup (wversionrequested, & wsadata );
If (Err! = 0 ){
/* Tell the user that we cocould not find a usable */
/* Winsock DLL .*/
Return;
}
/* Confirm that the Winsock DLL supports 2. 2 .*/
/* Note that if the DLL supports Versions greater */
/* Than 2.2 In addition to 2.2, it will still return */
/* 2.2 in wversion since that is the version we */
/* Requested .*/
If (lobyte (wsadata. wversion )! = 1 |
Hibyte (wsadata. wversion )! = 1 ){
/* Tell the user that we cocould not find a usable */
/* Winsock DLL .*/
Wsacleanup ();
Return;
}
/* The Winsock DLL is acceptable. Proceed .*/
// Create a server socket
Socket SVR = socket (af_inet, sock_dgram, 0 );
// Create an address
Sockaddr_in addrsvr;
Addrsvr. sin_family = af_inet;
Addrsvr. sin_port = htons (6000 );
Addrsvr. sin_addr.s_un.s_addr = inet_addr ("127.0.0.1 ");
Char recvbuf [128];
Char sendbuf [128];
Int Len = sizeof (sockaddr );
While (true)
{
Gets (sendbuf );
// Send data
Sendto (SVR, sendbuf, strlen (sendbuf) + 1,0, (sockaddr *) & addrsvr, Len );
// Receive data
Recvfrom (SVR, recvbuf, 0, (sockaddr *) & addrsvr, & Len );
Char * ipsvr = inet_ntoa (addrsvr. sin_addr );
Printf ("% s said: % s/n", ipsvr, recvbuf );
}
Closesocket (SVR );
Wsacleanup ();
}

 

Source of the original article (Click here)

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.