Windows network Programming "003" to achieve a simple wsasocket C/s Structure __ programming

Source: Internet
Author: User

When running, start the server side first.

/*
* Server-side
*/

#include < Winsock2.h >
#include < stdio.h >
#include < iostream.h >
#pragma comment (lib, "Ws2_32.lib")

SOCKET socksvc; Server-side sockets
SOCKET Sockconnect; Socket used for connection

DWORD WINAPI RecvData (LPVOID lpparameter); Functions that receive threads

int main ()
{
WORD wversionrequested;//define socket1.1 or socket2.0
Wsadata Wsadata; To define a variable that loads the socket version
int err; Error variable

wversionrequested = Makeword (2,2); Define connection as socket2.0

Err = WSAStartup (wversionrequested, &wsadata); Mount Socket2.0 Support
if (0!= Err)//To determine whether the mount succeeds
{
return-1;
}

if (Lobyte (wsadata.wversion)!= 2 | | Hibyte (wsadata.wversion)!= 2)//Determine the version number, whether or not the same as defined
{
WSACleanup (); If there is a problem, uninstall support and end the program return-1
return-1;
}

Socksvc = WSASocket (af_inet,sock_stream,0,null,0,wsa_flag_overlapped);
/*

SOCKET wsasocket (int af, int type, int protocol,
Lpwsaprotocol_info Lpprotocolinfo,
GROUP g, DWORD dwflags)

Socket socket (int AF, int type, int protocol)
Parameters: Address family, Socket type, and protocol type
These three factors collectively determine the service provider that creates the socket

The functions of the transport service provider include establishing a connection, transmitting data, realizing flow control and error control.
Ws2_32.dll implements the media functionality between the application and the service provider


First
Both the socket () and the WSASocket () function return a socket socket;
The socket () is implemented synchronously, and the routines in the socket library are blocked when applied to the blocking socket interface.
WSASocket () is used for asynchronous transmissions, and both send and receive operations of WSASocket () can be overlapped.

The receive function of the WSASocket () can be invoked multiple times, issuing a receive buffer, ready to receive incoming data.
The sending function can also be called multiple times to form a send buffer queue.
The socket () can only be sent after waiting for the message to do the next step.
Secondly
The following versions are supported by WSASocket ():
Version:requires Windows Sockets2.0.
Header:declared in Winsock2.h.
The socket () supports older versions, such as Windows Sockets1.0 and
Version:requires Windows Sockets1.1
*/

Sockaddr_in addrsvc; Server address Information
Addrsvc.sin_addr. S_un. S_ADDR = htonl (Inaddr_any); 0 Receive all Packets
addrsvc.sin_family = af_inet;
Addrsvc.sin_port = htons (4000);//Listening port

Bind (Socksvc, (sockaddr*) &addrsvc,sizeof (sockaddr)); Binding Services and Ports

Listen (socksvc,50);
/*
int Listen (SOCKET s, int users);
The service program can invoke the Listen function to make its stream socket s in a listening state. The stream socket s in the listening state will dimension
Protects a client connection request queue, which holds up to the users client connection requests. If the function is executed successfully,
Returns 0, or socket_error if execution fails.

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.