一、socket編程
公司主營業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出蒲江縣免費(fèi)做網(wǎng)站回饋大家。
先運(yùn)行服務(wù)器,本文采用線程來運(yùn)行服務(wù)器,再運(yùn)行客戶端。
二、基本概念
ip:可唯一標(biāo)識(shí)網(wǎng)絡(luò)上的一個(gè)主機(jī);
協(xié)議+端口:可唯一標(biāo)識(shí)主機(jī)中一個(gè)進(jìn)程;
所以通過IP+協(xié)議+端口三元組來標(biāo)識(shí)主機(jī)中的進(jìn)程。
三、編程實(shí)現(xiàn)
#pragma once #define WIN32 #include#include #include "stdafx.h" #include #include #pragma comment(lib, "ws2_32.lib") using namespace std; //typedef int(*DLLFunc)(int,int);//int是該方法形參的類型,有幾個(gè)參數(shù)就定義幾個(gè)。 //typedef int(*DLLFunc2)(); DWORD WINAPI ThreadFunc(HANDLE Thread) { //HINSTANCE hInstLibrary = LoadLibrary(_T("E:\\V3.0\\新建文件夾\\MyDll.dll"));//要寫清楚路徑,注意雙斜杠 //if (GetLastError() != 0) //{ // std::cout << GetLastError();//打印失敗信息 //} //if (hInstLibrary == NULL) //{ // FreeLibrary(hInstLibrary); // return 0; //} //DLLFunc2 dllFunc3; //dllFunc3 = (DLLFunc2)GetProcAddress(hInstLibrary, "SocketSevert"); //int i3 = dllFunc3(); //cout << i3 << endl; //FreeLibrary(hInstLibrary); //服務(wù)器端 0成功;22套接字;3綁定;4監(jiān)聽 WORD sockVersion = MAKEWORD(2, 2); WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0) { printf("-3"); //return -3; } // 創(chuàng)建通信端點(diǎn):套接字 int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { printf("服務(wù)器創(chuàng)建套接字失敗!\n"); } else { printf("服務(wù)器創(chuàng)建套接字成功!\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); //綁定 int err_log = bind(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr)); if (err_log != 0) { printf("服務(wù)器綁定失敗!\n"); } else { printf("服務(wù)器綁定成功!\n"); } //監(jiān)聽 err_log = listen(sockfd, 10); if (err_log != 0) { printf("服務(wù)器監(jiān)聽失??!\n"); } else { printf("服務(wù)器監(jiān)聽成功!\n"); } Sleep(10); int i = 0; while (1) { i++; struct sockaddr_in client_addr; char cli_ip[INET_ADDRSTRLEN] = ""; socklen_t cliaddr_len = sizeof(client_addr); //成功返回一個(gè)新的socket文件描述符,用于和客戶端通信,失敗返回-1 //表示三方握手完成,下一步服務(wù)器調(diào)用accept()接受連接 int connfd = accept(sockfd, (struct sockaddr*)&client_addr, &cliaddr_len); if (connfd < 0) { printf("accept第%d次失敗\n",i); continue; } else { printf("accept第%d次成功\n", i); } //接收數(shù)據(jù) char recv_buf[512] = { 0 }; while (recv(connfd, recv_buf, sizeof(recv_buf), 0) > 0) { i = sizeof(recv_buf); while (i--) printf("接收數(shù)據(jù):\n%c\n",recv_buf[i]); } } return 0; } int main() { //DLLFunc2 dllFunc2; //// hdll=LoadLibraryEx("*.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH),若dll庫中有其他dll的調(diào)用,就使用此語句 //HINSTANCE hInstLibrary = LoadLibrary(_T("E:\\V3.0\\新建文件夾\\MyDll.dll"));//要寫清楚路徑,注意雙斜杠 //if (GetLastError() != 0) //{ // std::cout << GetLastError();//打印失敗信息 //} //if (hInstLibrary == NULL) //{ // FreeLibrary(hInstLibrary); // return 0; //} //線程---服務(wù)器 HANDLE Thread; DWORD dwThreadId; Thread = ::CreateThread(NULL, 0, ThreadFunc, NULL, 0, &dwThreadId); //cout << "The new thread ID is :" << dwThreadId << endl; //客戶端 WORD sockVersion = MAKEWORD(2, 2); WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0) { return -1; } SOCKET sockClient = NULL; SOCKADDR_IN addrSrv; //addrSrv.sin_addr.S_un.S_addr = 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); //創(chuàng)建套接字 sockClient = socket(AF_INET, SOCK_STREAM, 0); if (sockClient < 0) { printf("創(chuàng)建套接字失??!\n"); } else { printf("創(chuàng)建套接字成功!\n"); } //網(wǎng)絡(luò)連接 if (connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)) == SOCKET_ERROR) { printf("connect失敗!\n"); } else { printf("網(wǎng)絡(luò)連接成功!\n"); char send_buf[12] = { 'c','d' }; int nRecv = ::send(sockClient, send_buf, sizeof(send_buf), 0); if (nRecv < 0) { printf("發(fā)送失??!\n"); } else { printf("發(fā)送成功!\n"); } } //SocketConnect是DLL庫里定義的方法 //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; }