基于TCP(面向连接)的Socket编程
基于TCP(面向连接)的Socket编程
一、客户端:
1、打开一个套接字(Socket);
2、发起连接请求(connect);
3、如果连接成功,则进行数据交换(read、write、send、recv);
4、数据交换完成,关闭连接(shutdown、close);
二、服务器端:
1、打开一个套接字(Socket);
2、将套接字绑定到服务器地址上(bind);
3、指定套接字为服务器套接字(listen),做好连接请求准备;
4、等待连接请求(connect);
5、如果连接请求到,则连接建立,进行数据交换(read、write、send、recv);
6、数据交换完成,关闭连接(shutdown、close);
基于UDP(面向无连接)的Socket编程
一、客户端\服务器端:
1、打开一个套接字(Socket);
2、将套接字绑定到指定的服务器地址和端口上(bind);
3、进行数据交换(read、write、send、recv);
4、数据交换完成,关闭连接(shutdown、close);
三、MFC对Socket的支持:
1、创建CAsyncSocket对象;
2、发送接收数据报(SendTo、RecvFrom);
3、连接服务器(Connect);
4、接收连接(Listen);
5、发送和接收流式数据(Send、Receive);
6、关闭套接字(Close);
7、差错处理(GetLastError)。
四、实例:网络聊天工具
客户端:
1、。。。。
2、利用类向导重载CAsyncSocket类,生成新的MySocket类。。
3、利用类向导重载CAsyncSocket的OnReceive(int nErrorCode)和OnSend(int nErrorCode)、OnConnect(int nErrorCode)函数。
void MySocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
//获取对话框指针
CTestApp*pApp=(CTestApp*)AfxGetApp();
CTestDlg*pDlg=(CTestDlg*)pApp->m_pMainWnd;
//往编辑框中插入消息
char *pbuf=new char[4096];
int ibufsize=4096;
int ircvd;
CString strrecvd;
ircvd=Receive(pbuf,ibufsize);
if(ircvd==SOCKET_ERROR)
{
pDlg->MessageBox("SOCKET_ERROR");
}
else
{
pbuf[ircvd]=NULL;
pDlg->m_recmsg+="服务器:";
pDlg->m_recmsg+=pbuf;
pDlg->m_recmsg+="\r\n";
pDlg->RefreshScreen();
}
delete pbuf;
CAsyncSocket::OnReceive(nErrorCode);
}
void MySocket::OnSend(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CAsyncSocket::OnSend(nErrorCode);
}
void MySocket::OnConnect(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CTestApp*pApp=(CTestApp*)AfxGetApp();
CTestDlg*pDlg=(CTestDlg*)pApp->m_pMainWnd;
int iResult=nErrorCode;
CString buffer;
int namelen;
if(iResult!=0)
{
buffer.Format("连接服务器失败。\r\n");
pDlg->m_recmsg+=buffer;
}
else
{
namelen=sizeof(sockaddr_in);
buffer.Format("成功连接到服务器 %s:%d.\r\n",pDlg->m_ipstr,pDlg->m_port);
pDlg->m_recmsg+=buffer;
pDlg->GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDOK)->EnableWindow(TRUE);
}
pDlg->RefreshScreen();
CAsyncSocket::OnConnect(nErrorCode);
}
4、在C*Dlg类中
头文件中:
#include "MySocket.h"
MySocket m_socket;
CString m_ipstr;
.CPP文件中:
void CTestDlg::OnSend()
{
// TODO: Add your control notification handler code here
int ilen;
int isent;
UpdateData(TRUE);
if(m_msg!="")
{
ilen=m_msg.GetLength ();
isent=m_socket.Send(LPCTSTR(m_msg),ilen);
if(isent==SOCKET_ERROR)
{
MessageBox("连接失败,请重试!");
// connect=false;
}
else
{
m_recmsg+="客户机:"+m_msg;
m_recmsg+="\r\n";
UpdateData(FALSE);
}
}
}
void CTestDlg::RefreshScreen()
{
UpdateData(false);
}
void CTestDlg::OnConnect()
{
if (!AfxSocketInit())
{
AfxMessageBox("IDP_SOCKETS_INIT_FAILED");
return ;
}
GetDlgItemText(IDC_IPADDRESS1,m_ipstr);
m_socket.m_hSocket=INVALID_SOCKET;
UpdateData(true);
BOOL flag=m_socket.Create();
if(!flag)
{
AfxMessageBox("SOCKET ERROR");
return;
}
m_socket.Connect(m_ipstr,m_port);
}
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
m_socket.Close();
CDialog::OnOK();
}
服务器端:
1、。。。。
2、利用类向导重载CAsyncSocket类,生成新的MySocket类。。
3、利用类向导重载CAsyncSocket的OnReceive(int nErrorCode)和OnSend(int nErrorCode)函数。
void MySocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CTestApp*pApp=(CTestApp*)AfxGetApp();
CTestDlg*pDlg=(CTestDlg*)pApp->m_pMainWnd;
//往列表框中插入消息
char *pbuf=new char[4096];
int ibufsize=4096;
int ircvd;
CString strrecvd;
ircvd=Receive(pbuf,ibufsize);
if(ircvd==SOCKET_ERROR)
{
pDlg->MessageBox("SOCKET_ERROR");
}
else
{
pbuf[ircvd]=NULL;
pDlg->m_msg+="客户机:";
pDlg->m_msg+=pbuf;
pDlg->m_msg+="\r\n";
pDlg->RefreshScreen();
}
delete pbuf;
CAsyncSocket::OnReceive(nErrorCode);
}
void MySocket::OnSend(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CAsyncSocket::OnSend(nErrorCode);
}
4、新建MyServerSocket类,并添加MySocket * m_socket;即接收请求后的套接字指针。
MySocket * m_socket;
void CMyServerSocket::OnAccept(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CTestApp*pApp=(CTestApp*)AfxGetApp();
CTestDlg*pDlg=(CTestDlg*)pApp->m_pMainWnd;
//显示连接消息
pDlg->m_msg="客户机连接到服务器";
pDlg->m_msg+="\r\n";
pDlg->RefreshScreen();
pDlg->GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDOK)->EnableWindow(TRUE);
MySocket* psocket=new MySocket();
if(Accept(*psocket))
{
psocket->AsyncSelect(FD_READ);
m_socket=psocket;
}
else
{
delete psocket;
}
CAsyncSocket::OnAccept(nErrorCode);
}
5、C*Dlg类中
void CTestDlg::RefreshScreen()
{
UpdateData(false);
}
void CTestDlg::OnListen() //创建服务器
{
// TODO: Add your control notification handler code here
UpdateData(true);
if (!AfxSocketInit())
{
AfxMessageBox("IDP_SOCKETS_INIT_FAILED");
return ;
}
BOOL flag=m_serversocket.Create(m_port);
if(!flag)
{
AfxMessageBox("SOCKET ERROR");
return;
}
flag=m_serversocket.Listen(1);
if(!flag)
{
AfxMessageBox("SOCKET ERROR");
return;
}
SetDlgItemText(IDC_LISTEN,"正在监听");
}
void CTestDlg::OnSend()
{
// TODO: Add your control notification handler code here
int ilen;
int isent;
UpdateData(TRUE);
if(m_sendmsg!="")
{
ilen=m_sendmsg.GetLength ();
isent=m_serversocket.m_socket->Send(LPCTSTR(m_sendmsg),ilen);
if(isent==SOCKET_ERROR)
{
MessageBox("连接失败,请重试!");
}
else
{
m_msg+="服务器:"+m_sendmsg;
m_msg+="\r\n";
UpdateData(FALSE);
}
}
}
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
m_sendmsg="服务器退出";
UpdateData(false);
OnSend();
m_serversocket.Close();
CDialog::OnOK();
}
基于TCP(面向连接)的Socket编程相关推荐
- 基于TCP/UDP的socket编程
基于TCP(面向连接)的socket编程 服务器端顺序: 1. 创建套接字(socket) 2. 将套接字绑定到一个本地地址和端口上(bind) 3. 将套接字设为监听模式,准备接收客户请求(l ...
- tcp java实例_实现了基于TCP的Java Socket编程实例代码
实现了基于TCP的Java Socket编程,功能很简单:客户端向服务器端输出一名话"connect",服务器端接收输出到控制台并向客户端输出一名话"Hello" ...
- java socket 通信协议_java 基于TCP协议的Socket编程和通信
java 基于 TCP 协议的 Socket 编程和通信 在网络通讯中,第一次主动发起通讯的程序被称作客户 端 (Client) 程序, 简称客户端, 而在第一次通讯中等待连接的 程序被称作服务器端 ...
- delphi socket 流的使用_基于TCP协议的Socket编程和通信_单向通信
1.TCP:单向通信Socket之服务器端 import java.io.BufferedWriter; import java.io.IOException; import java.io.Outp ...
- python 网络编程 套接字的初使用 基于TCP协议的socket
文章目录 基于TCP协议的socket server端 client端 尝试启动 基于TCP协议的socket tcp是基于链接的,必须先启动服务端,然后再启动客户端去链接服务端 server端 # ...
- java socket发送定长报文_一个基于TCP协议的Socket通信实例
原标题:一个基于TCP协议的Socket通信实例 1. 前言 一般接口对接多以http/https或webservice的方式,socket方式的对接比较少并且会有一些难度.正好前段时间完成了一个so ...
- 基于TCP协议的Socket网络通信
前言 一. 什么是网络(了解七层网络模型)? 二. 什么是TCP/UDP协议? 三.什么是socket? 定义 四.基于TCP协议的socket通信的实现步骤是怎样的? 客户端的实现 服务端的实现 测 ...
- Java 基于 TCP/IP 实现 Socket中的多客户端通信
使用多线程实现多客户端的通信功能, Client.java(客户端)同上一节中的一致,不需要修改 Server.java package com.learn;import java.io.IOExce ...
- Sockey编程之基于 UDP 协议的 Socket 编程
一.基于 UDP 协议的 Socket 编程 1.端口选择 已知端口:0~1023,为固定服务保留. 已注册的端口:1024~49151,供普通用户的普通用户进程或程序使用. 动态或私用端口: 491 ...
最新文章
- mac地址修改_快速更改WiFi MAC地址
- k8s pod重启策略:Always、OnFailure、Never配置示例
- destoon 屏蔽会员组,让个人,游客不显示
- Java技术:Mybatis-plus常用API全套教程,值得收藏!
- EduSoHo精品在线教育点播系统网站源码
- 互联网java面试_互联网java面试宝典
- 全国院线总票房破 50 亿!影院复工后,哪些电影最受欢迎?
- CF813E Army Creation
- @RequestParam今天才知道是咋用的..
- 多线程之 interrupt,interrupted,isInterrupted 方法区别
- MATPOWER 修改数据格式和应用
- 什么是生成式对抗神经网络GAN
- java抓取豆瓣网页内容_爬虫实践--豆瓣电影当前上映电影信息爬取
- Maven仓库的下载和配置settings.xml文件
- WebUploader 设置单个图片上传
- 2022年10月 使用win11系统自带远程桌面,远程控制VMware虚拟机系统
- java里面的悲观锁和乐观锁
- 10-不用加减乘除做加法
- 考计算机基础a的ap考试,用AP考试,敲开计算机名校大门!
- 数据库与MPP数仓(十五):MPP的架构与选型