socket网络编程--基于windows
一、socket编程
公司主营业务:成都网站设计、网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出蒲江县免费做网站回馈大家。
先运行服务器,本文采用线程来运行服务器,再运行客户端。
二、基本概念
ip:可唯一标识网络上的一个主机;
协议+端口:可唯一标识主机中一个进程;
所以通过IP+协议+端口三元组来标识主机中的进程。
三、编程实现
#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是该方法形参的类型,有几个参数就定义几个。 //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); //服务器端 0成功;22套接字;3绑定;4监听 WORD sockVersion = MAKEWORD(2, 2); WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0) { printf("-3"); //return -3; } // 创建通信端点:套接字 int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { printf("服务器创建套接字失败!\n"); } else { printf("服务器创建套接字成功!\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("服务器绑定失败!\n"); } else { printf("服务器绑定成功!\n"); } //监听 err_log = listen(sockfd, 10); if (err_log != 0) { printf("服务器监听失败!\n"); } else { printf("服务器监听成功!\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); //成功返回一个新的socket文件描述符,用于和客户端通信,失败返回-1 //表示三方握手完成,下一步服务器调用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); } //接收数据 char recv_buf[512] = { 0 }; while (recv(connfd, recv_buf, sizeof(recv_buf), 0) > 0) { i = sizeof(recv_buf); while (i--) printf("接收数据:\n%c\n",recv_buf[i]); } } return 0; } int main() { //DLLFunc2 dllFunc2; //// hdll=LoadLibraryEx("*.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH),若dll库中有其他dll的调用,就使用此语句 //HINSTANCE hInstLibrary = LoadLibrary(_T("E:\\V3.0\\新建文件夹\\MyDll.dll"));//要写清楚路径,注意双斜杠 //if (GetLastError() != 0) //{ // std::cout << GetLastError();//打印失败信息 //} //if (hInstLibrary == NULL) //{ // FreeLibrary(hInstLibrary); // return 0; //} //线程---服务器 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); //创建套接字 sockClient = socket(AF_INET, SOCK_STREAM, 0); if (sockClient < 0) { printf("创建套接字失败!\n"); } else { printf("创建套接字成功!\n"); } //网络连接 if (connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)) == SOCKET_ERROR) { printf("connect失败!\n"); } else { printf("网络连接成功!\n"); char send_buf[12] = { 'c','d' }; int nRecv = ::send(sockClient, send_buf, sizeof(send_buf), 0); if (nRecv < 0) { printf("发送失败!\n"); } else { printf("发送成功!\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; }
名称栏目:socket网络编程--基于windows
本文链接:http://scyanting.com/article/jigcss.html