设为首页 收藏本站
查看: 1504|回复: 0

[经验分享] socket网络编程--基于windows

[复制链接]
累计签到:4 天
连续签到:1 天
发表于 2018-6-18 07:07:58 | 显示全部楼层 |阅读模式
#pragma once  
#define WIN32
  
#include<iostream>
  
#include<stdio.h>
  
#include &quot;stdafx.h&quot;
  
#include<sys/types.h>
  
#include<WS2tcpip.h>
  
#pragma comment(lib, &quot;ws2_32.lib&quot;)
  
using namespace std;
  
//typedef int(*DLLFunc)(int,int);//int是该方法形参的类型,有几个参数就定义几个。
  
//typedef int(*DLLFunc2)();
  

  

  
DWORD WINAPI ThreadFunc(HANDLE Thread)
  
{
  //HINSTANCE hInstLibrary = LoadLibrary(_T(&quot;E:\\V3.0\\新建文件夹\\MyDll.dll&quot;));//要写清楚路径,注意双斜杠
  //if (GetLastError() != 0)
  //{
  //std::cout << GetLastError();//打印失败信息
  //}
  //if (hInstLibrary == NULL)
  //{
  //FreeLibrary(hInstLibrary);
  //return 0;
  //}
  

  //DLLFunc2 dllFunc3;
  //dllFunc3 = (DLLFunc2)GetProcAddress(hInstLibrary, &quot;SocketSevert&quot;);
  //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(&quot;-3&quot;);
  //return -3;
  }
  // 创建通信端点:套接字
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
  {
  printf(&quot;服务器创建套接字失败!\n&quot;);
  }
  else
  {
  printf(&quot;服务器创建套接字成功!\n&quot;);
  }
  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(&quot;服务器绑定失败!\n&quot;);
  }
  else
  {
  printf(&quot;服务器绑定成功!\n&quot;);
  }
  

  //监听
  err_log = listen(sockfd, 10);
  if (err_log != 0)
  {
  printf(&quot;服务器监听失败!\n&quot;);
  }
  else
  {
  printf(&quot;服务器监听成功!\n&quot;);
  }
  Sleep(10);
  int i = 0;
  while (1)
  {
  i++;
  struct sockaddr_in client_addr;
  char cli_ip[INET_ADDRSTRLEN] = &quot;&quot;;
  socklen_t cliaddr_len = sizeof(client_addr);
  

  //成功返回一个新的socket文件描述符,用于和客户端通信,失败返回-1
  //表示三方握手完成,下一步服务器调用accept()接受连接
  int connfd = accept(sockfd, (struct sockaddr*)&client_addr, &cliaddr_len);
  if (connfd < 0)
  {
  printf(&quot;accept第%d次失败\n&quot;,i);
  continue;
  }
  else
  {
  printf(&quot;accept第%d次成功\n&quot;, i);
  }
  //接收数据
  char recv_buf[512] = { 0 };
  while (recv(connfd, recv_buf, sizeof(recv_buf), 0) > 0)
  {
  i = sizeof(recv_buf);
  while (i--)
  printf(&quot;接收数据:\n%c\n&quot;,recv_buf);
  }
  }
  return 0;
  
}
  

  
int main()
  
{
  //DLLFunc2 dllFunc2;
  

  //// hdll=LoadLibraryEx(&quot;*.dll&quot;, NULL, LOAD_WITH_ALTERED_SEARCH_PATH),若dll库中有其他dll的调用,就使用此语句
  //HINSTANCE hInstLibrary = LoadLibrary(_T(&quot;E:\\V3.0\\新建文件夹\\MyDll.dll&quot;));//要写清楚路径,注意双斜杠
  //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 << &quot;The new thread ID is :&quot; << 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(&quot;127.0.0.1&quot;);
  inet_pton(AF_INET, &quot;128.0.0.1&quot;, (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(&quot;创建套接字失败!\n&quot;);
  }
  else
  {
  printf(&quot;创建套接字成功!\n&quot;);
  }
  //网络连接
  if (connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)) == SOCKET_ERROR)
  {
  printf(&quot;connect失败!\n&quot;);
  }
  else
  {
  printf(&quot;网络连接成功!\n&quot;);
  char send_buf[12] = { 'c','d' };
  int nRecv = ::send(sockClient, send_buf, sizeof(send_buf), 0);
  if (nRecv < 0)
  {
  printf(&quot;发送失败!\n&quot;);
  }
  else
  {
  printf(&quot;发送成功!\n&quot;);
  }
  }
  //SocketConnect是DLL库里定义的方法
  //dllFunc2 = (DLLFunc2)GetProcAddress(hInstLibrary, &quot;SocketConnect&quot;);
  //if (dllFunc2 == NULL)
  //{
  //FreeLibrary(hInstLibrary);
  //return 0;
  //}
  //int i2 = dllFunc2();
  //cout << i2 << endl;
  //FreeLibrary(hInstLibrary);
  

  ::WaitForSingleObject(Thread, INFINITE);
  ::CloseHandle(Thread);
  return 0;
  
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-525173-1-1.html 上篇帖子: Linux中挂载Windows里共享目录 下篇帖子: Windows 10的市场占有率真的超过Windows 7了吗?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表