Socket network Programming-Windows-based

Source: Internet
Author: User
Tags htons

First, socket programming

Run the server first, this article uses threads to run the server, and then run the client.

Ii. Basic Concepts

IP: Can uniquely identify a host on the network;

Protocol + port: can uniquely identify a process in the host;

So the process in the host is identified by ip+ Protocol + port triples.

Third, the realization of programming

#pragma  once#define WIN32#include<iostream> #include <stdio.h> #include   "stdafx.h" # include<sys/types.h> #include <WS2tcpip.h> #pragma  comment (lib,  "Ws2_32.lib") using  Namespace std;//typedef int (*dllfunc) (int,int);//int is the type of the method parameter, with several parameters defined. Typedef int (*DLLFUNC2) ();D Word winapi threadfunc (handle thread) {//HINSTANCE  Hinstlibrary = loadlibrary (_t ("e:\\v3.0\\ new Folder \\MyDll.dll"));//To write clear path, note double slash//if  (GetLastError ()  != 0)//{//std::cout << getlasterror ();//Print Failure message//}//if  (hinstlibrary ==  null)//{//freelibrary (hinstlibrary);//return 0;//}//dllfunc2 dllfunc3;//dllfunc3 =  ( DLLFUNC2) GetProcAddress (hinstlibrary,  "Socketsevert");//int i3 = dllfunc3 ();//cout  << i3 << endl;//freelibrary (hinstlibrary);//server-side   0 succeeded; 22 sockets; 3 bindings; 4 Listening word  sockversion = makeword (2,&NBSP;2); Wsadata wsaData;if  (WSAStartup (sockversion, &wsadata)  != 0) {printf ("-3");//return - 3;}   Create communication endpoint: Socket Int sockfd = socket (af_inet, sock_stream, 0);if  (sockfd  < 0) {printf ("the server created the socket failed!") \ n ");} else{printf ("Server created socket succeeded! \ n ");} Struct sockaddr_in my_addr;my_addr.sin_family = af_inet;my_addr.sin_port = htons (8888); my_addr.sin_addr.s_addr = htonl (inaddr_any);//Binding Int err_log = bind (SOCKFD,   (struct sockaddr*) &my_addr, sizeof (MY_ADDR));if  (err_log != 0) {printf (" Server bindings failed! \ n ");} else{printf ("Server bindings succeeded! \ n ");} Monitor Err_log = listen (sockfd, 10);if  (err_log != 0) {printf ("Server Listener failed!") \ n ");} else{printf ("Server monitoring succeeded! \ n ");} Sleep (;int i = 0;while ) (1) {I++;struct sockaddr_in client_addr;char cli_ ip[inet_addrstrlen] =  ""; socklen_t cliaddr_len = sizeof (client_addr);//Successful return of a new sockThe ET file descriptor, used to communicate with the client, failed to return -1//indicates that the three-party handshake is complete, and the next server calls accept () accepts the connection int connfd = accept (sockfd,  (struct  sockaddr*) &client_addr, &cliaddr_len);if  (connfd < 0) {printf ("Accept% D-time failure \ n ", i); continue;} else{printf ("Accept%d successes \ n",  i);} Receive Data char recv_buf[512] = { 0 };while  (recv (connfd, recv_buf, sizeof ( RECV_BUF),  0)  > 0) {i = sizeof (recv_buf);while  (i--) printf ("Receive data: \n%c\n", recv_ Buf[i]);}} return 0;} Int main () {//dllfunc2 dllfunc2;//// hdll=loadlibraryex ("*.dll",  null, load_with_ Altered_search_path), use this statement//hinstance hinstlibrary = loadlibrary (_t ("e:\\v3.0\") if there are calls to other DLLs in the DLL library. \ New Folder \\MyDll.dll "));//To write clear path, note double slash//if  (GetLastError ()  != 0)//{//std::cout <<  GetLastError ();//Print failure Information//}//if  (hinstlibrary == null)//{//freelibrary (hinstlibrary);//return  0;//}//Thread---server handle thread;dword&nBsp;dwthreadid; Thread = ::createthread (Null, 0, threadfunc, null, 0, &dwthreadid);// cout <<  "THE&NBSP;NEW&NBSP;THREAD&NBSP;ID&NBSP;IS&NBSP;:"  << dwThreadId  << endl;//client Word sockversion = makeword (2,&NBSP;2); wsadata wsadata;if  (WSAStartup (sockversion, &wsadata)  != 0) {return -1;} socket sockclient = null; Sockaddr_in addrsrv;//addrsrv.sin_addr. S_un. S_ADDR&NBSP;=&NBSP;INET_ADDR ("127.0.0.1"); Inet_pton (af_inet,  "128.0.0.1",  (void*) &addrsrv.sin_ Addr. S_un. S_ADDR); addrsrv.sin_family = af_inet;addrsrv.sin_port = htons (8888);//Create Socket Sockclient  = socket (af_inet, sock_stream, 0);if  (sockclient < 0) {printf ("Create socket failed! \ n ");} else{printf ("Create socket succeeded! \ n ");} Network connection if  (Connect (sockclient,  (sockaddr*) &addrsrv, sizeof (sockaddr))  == socket_ ERROR) {printf ("Connect lostDefeat! \ n ");} else{printf ("Network connection is successful! \ n ");char send_buf[12] = {  ' C ', ' d '  };int nrecv = ::send (SockClient,  send_buf, sizeof (SEND_BUF),  0);if  (nrecv < 0) {printf ("Send failed! \ n ");} ELSE{PRINTF ("Sent successfully! \ n ");}} Socketconnect is a method defined in the DLL library//dllfunc2 =  (DLLFUNC2) GetProcAddress (hinstlibrary,  "Socketconnect") ;//if  (dllfunc2 == null)//{//freelibrary (hinstlibrary);//return 0;//}//int i2 =  dllfunc2 ();//cout << i2 << endl;//freelibrary (HInstLibrary);:: WaitForSingleObject (Thread, infinite);:: CloseHandle (Thread); return 0;}


Socket network Programming-Windows-based

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.