c++ 连接服务器源码,c++ socket最简单实例源码(含服务端以及客户端)
【实例简介】
【实例截图】
【核心代码】
// ServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//全局变量
CWinThread *pThread = NULL;
bool m_exit = false;
unsigned int StartServer(LPVOID lParam)
{
//初始化Winscok
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return 1;
}
m_exit = false;
CServerDlg *aDlg = (CServerDlg *)lParam;
//获取端口
CString strPort;
aDlg->GetDlgItemText(IDC_EDIT_PORT, strPort);
UINT nPort = atoi(strPort);
//socket----创建2个socket--------------------------------------------
CSocket aSocket, serverSocket;
//最好不要使用aSocket.Create创建,因为容易会出现10048错误
if (!aSocket.Socket())
{
char szError[256] = {0};
sprintf(szError, "Create Faild: %d", GetLastError());
AfxMessageBox(szError);
return 1;
}
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);
//设置Socket的选项, 解决10048错误必须的步骤
aSocket.SetSockOpt(SO_REUSEADDR, (void *)&bOptVal, bOptLen, SOL_SOCKET);
//绑定
if (!aSocket.Bind(nPort))
{
char szError[256] = {0};
sprintf(szError, "Bind Faild: %d", GetLastError());
AfxMessageBox(szError);
return 1;
}
//监听
if(!aSocket.Listen(10))
{
char szError[256] = {0};
sprintf(szError, "Listen Faild: %d", GetLastError());
AfxMessageBox(szError);
return 1;
}
CString strText;
aDlg->GetDlgItemText(IDC_EDIT_LOG, strText);
strText = "服务已经开启! \r\n";
aDlg->SetDlgItemText(IDC_EDIT_LOG, strText);
while(!m_exit)
{
//接收外部连接
if(!aSocket.Accept(serverSocket))
{
continue;
}
else
{
char szRecvMsg[256] = {0};
char szOutMsg[256] = {0};
serverSocket.Receive(szRecvMsg, 256); //接收客户端内容:阻塞
sprintf(szOutMsg, "接受到的参数是: %s \r\n", szRecvMsg);
aDlg->GetDlgItemText(IDC_EDIT_LOG, strText);
strText = szOutMsg;
aDlg->SetDlgItemText(IDC_EDIT_LOG, strText);
serverSocket.Send("服务器已经收到,已经做出操作!", 50);//发送内容给客户端
serverSocket.Close();//关闭
}
}
aSocket.Close();
serverSocket.Close();
aDlg->GetDlgItemText(IDC_EDIT_LOG, strText);
strText = "Have Close!";
aDlg->SetDlgItemText(IDC_EDIT_LOG, strText);
return 0;
}
void StopServer()
{
m_exit = true;
}
/
// CServerDlg dialog
CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CServerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CServerDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CServerDlg message handlers
BOOL CServerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CServerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon 1) / 2;
int y = (rect.Height() - cyIcon 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CServerDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
pThread = new CWinThread(StartServer, (LPVOID)this);
pThread->CreateThread(CREATE_SUSPENDED);
pThread->ResumeThread();
SetDlgStatus(true);
}
void CServerDlg::OnButtonClose()
{
// TODO: Add your control notification handler code here
StopServer();
pThread->SuspendThread();
delete pThread;
pThread = NULL;
SetDlgStatus(false);
}
void CServerDlg::SetDlgStatus(bool isOpen)
{
this->GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(!isOpen);
this->GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(isOpen);
this->GetDlgItem(IDC_EDIT_PORT)->EnableWindow(!isOpen);
}
c++ 连接服务器源码,c++ socket最简单实例源码(含服务端以及客户端)相关推荐
- 多线程+SOCKET编程实现qq群聊的服务端和客户端
多线程+SOCKET编程实现qq群聊的服务端和客户端 标签(空格分隔): 多线程 网络编程 线程同步 一.设计思路 1.服务端 每来一个客户端连接,服务端起一个线程维护: 将收到的消息转发给所有的客户 ...
- Socket通信C#项目,完整的服务端和客户端
Socket通信C#项目,完整的服务端和客户端,让您绕过最难写的Socket管理,是困难的多线程处理变成简单的事件处理,非常容易上手. 功能带有断线重连,实时侦测设备状态,简单实用,适合初学者或有迫切 ...
- 诛仙3el服务器位置,【诛仙3风吟】虚拟机镜像一键服务端+配套客户端+青萝+EL编辑器+GM工具+启动说明...
[诛仙3风吟]虚拟机镜像一键服务端+配套客户端+青萝+EL编辑器+GM工具+启动说明_站长网(Downzz.com) 资源介绍: 1.修复部分bug. 2.添加官方12月圣诞2018时装.坐骑.(已收 ...
- socket文件传输服务器,Socket文件传输(含服务端以及客户端源码)
Socket文件传输 [实例简介] 传输任意格式文件 [实例截图] [核心代码] using System; using System.Collections.Generic; using Syste ...
- java贪吃蛇客户端服务器_java Socket套接字TCP编程开发服务端和客户端之间的通信 - 贪吃蛇学院-专业IT技术平台...
超级简单,没有太多实质内容的Socket服务端,客户端小程序 先运行server 再运行client OK 服务端代码如下: public class Server { public static v ...
- 面试必备之:MFC socket编程(浅出+深度:服务端和客户端端口问题)
要写网络程序就必须用Socket,这是程序员都知道的.而且,面试的时候,我们也会问对方会不会Socket编程?一般来说,很多人都会说,Socket编程基本就是listen,accept以及send,w ...
- 手游服务器ip 修改密码,手游无双小师妹一键启动服务端+配套客户端+启动教程+IP修改说明等...
内容介绍 资源说明: 1.本资源默认IP为192.168.1.116 2.如本机为此IP则可按照启动教程直接启动服务端. 3.如本机IP为非此IP,请自行修改本机IP为192.168.1.116或参考 ...
- SVN 版本服务器搭配全过程详解(含服务端、客户端)
1.为什么要用VisualSVN Server,而不用Subversion? 回答: 因为如果直接使用Subversion,那么在Windows 系统上,要想让它随系统启动,就要封装SVN Serve ...
- SVN版本服务器搭配全过程详解(含服务端、客户端)
1.为什么要用VisualSVN Server,而不用Subversion? 回答: 因为如果直接使用Subversion,那么在Windows 系统上,要想让它随系统启动,就要封装SVN Serve ...
- Socket异步服务器,可以监控客户端的状态,功能有,文字测试,服务端向客户端传输屏幕录像(UDP传输)、监控客户端屏幕(UDP传输),抖动用户窗体、发送文件给用户、扫描客户的C盘目录。
用VS2015工具C#语言编写了一个简单的Socket异步服务器,可以监控客户端的状态,功能有,文字测试,服务端向客户端传输屏幕录像(UDP传输).监控客户端屏幕(UDP传输),抖动用户窗体.发送文件 ...
最新文章
- 数说真相|46公里内,Uber更划算
- ios 重复引用 静态库_iOS-如果静态库(.a)包含重复的目标文件(.o),会发生什么?
- POJ - 3268 Silver Cow Party(最短路)
- vue从入门到精通之进阶篇(一)vue-router:导航守卫
- Java高并发编程详解系列-线程安全数据同步
- Maven学习总结(18)——深入理解Maven仓库
- Ubuntu下Truffle框架的搭建 2022年最新版
- 让华泰浮盈10亿美金的AssetMark-统包资产管理平台价值几何?
- opencapwap简要分析
- 如果时光可以倒流,会不会有不一样的故事
- 数据挖掘第二章 认识数据
- 数据分析01 - 规范化方法
- 闲鱼无货源电商课程第19期【完结】
- qt中制作添加 .ts 翻译文件
- 缩减运算符,就是多输入门电路,见图
- linux系统找不到共享文件夹
- 简单的利用栈 将二进制转换成十六进制
- 如何设置页面的上边距和下边距
- 图解订单系统从 0 到 1 的设计思路
- VB实现向窗口发送按键消息