C#计算机网络编程实验期中报告.docx
- 文档编号:27175464
- 上传时间:2023-06-27
- 格式:DOCX
- 页数:27
- 大小:298.21KB
C#计算机网络编程实验期中报告.docx
《C#计算机网络编程实验期中报告.docx》由会员分享,可在线阅读,更多相关《C#计算机网络编程实验期中报告.docx(27页珍藏版)》请在冰豆网上搜索。
C#计算机网络编程实验期中报告
常熟理工学院
课程设计报告
课程C#网络通信程序设计
题目电子教室多媒体系统
学院计算机科学与工程学院
班级
学号
姓名
2017年11月8日
目 录
1需求分析
2概要设计
3详细设计与编码
4调试分析
5用户使用说明
6设计心得
7参考文献
一.需求分析
电子教室管理系统,方便老师监控学生屏幕,向学生发送课堂文件,将屏幕广播给学生,有利于学生学习老师在计算机上的操作。
对于提高教师授课与学生学习效率。
二.概要设计
分为服务器端(教师端)和客户机端(学生端):
1.服务器端功能
(1)屏幕监控
(2)屏幕广播
(3)文件传送
2.客户机端功能
(1)接受老师屏幕广播
(2)接受老师发的文件
三.详细设计与编码
设计思路:
在已经学习过的远程屏幕程序设计基础上,再添加广播功能,发送文件功能。
教师端可以做成Windows服务,在服务启动后,先创建启动线程,并在线程中实例化UDP套接字,实例化内存,然后循环地进行屏幕捕获(每次捕获得到一副图片)和图片数据广播。
学生端做成Windows应用程序,用户可以通过界面执行开始和停止操作,可以循环地接收服务器端发送的广播数据,并将图片显示到界面上。
由于隔一段时间发送一帧图像,当时间间隔小到一定程度时,客户端看到的就是连续的画面。
具体代码如下。
教师端:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
usingSystem.Net;
usingSystem.Net.Sockets;
usingSystem.IO;
usingSystem.Threading;
namespaceteacher
{
publicpartialclassForm1:
Form
{
//主要变量定义
Socketsocket=null;
Socketpara=null;
Socket[]parb=newSocket[4];
Socket[]parc=newSocket[4];
inti=0;
intj=0;
int[]ports;
int[]sendports=null;
PictureBox[]pbs;
int[]fileports=null;
Socketpard=null;
//API函数对BitBlt()和CreateDC()的引用
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
privatestaticexternIntPtrCreateDC
(
stringlpszDriver,
stringlpszDevice,
stringlpszOutput,
IntPtrlpInitData
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
privatestaticexternboolBitBlt
(
IntPtrhdcDest,
intnXDest,
intnYDest,
intnWidth,
intnHeight,
IntPtrhdcSrc,
intnXSrc,
intnYSrc,
System.Int32dwRop
);
publicForm1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls=false;
this.socket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
this.sendports=newint[]{6001,6002,6003,6004};
for(inta=0;a<4;a++)
{
Threadthdd=newThread(newParameterizedThreadStart(this.outscreen));
thdd.Start(a);
}
this.para=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
this.fileports=newint[]{7001,7002,7003,7004};
for(inta=0;a<4;a++)
{
Threadth=newThread(newParameterizedThreadStart(this.sendfile));
th.Start(a);
}
this.pard=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
this.ports=newint[]{8001,8002,8003,8004};
this.pbs=newPictureBox[]{this.pictureBox1,this.pictureBox2,this.pictureBox3,this.pictureBox4};
for(inti=0;i<4;i++)
{
Threadthd=newThread(newParameterizedThreadStart(this.getRemote));
thd.Start(i);
}
}
//屏幕捕获
publicvoidcapture()
{
IntPtrdc1=CreateDC("DISPLAY",null,null,(IntPtr)null);
Graphicsg1=Graphics.FromHdc(dc1);
System.Drawing.ImageMyImage=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,g1);
Graphicsg2=Graphics.FromImage(MyImage);
IntPtrdc3=g1.GetHdc();
IntPtrdc2=g2.GetHdc();
BitBlt(dc2,0,0,Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,dc3,0,0,13369376);
g1.ReleaseHdc(dc3);
g2.ReleaseHdc(dc2);
MemoryStreamms=newMemoryStream();
MyImage.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[]b=ms.GetBuffer();
for(intj=0;j
SendVarData(parb[j],b);
}
publicvoidgetRemote(Objectindex)
{
inti=(int)index;
Socketls=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
ls.Bind(newIPEndPoint(IPAddress.Any,this.ports[i]));
ls.Listen(10);
Socketds=null;
try
{
ds=ls.Accept();
while(true)
{
byte[]b=ReceiveVarData(ds);
Imagemyimage=Image.FromStream(newMemoryStream(b));
this.pbs[i].Image=myimage;
this.pbs[i].SizeMode=System.Windows.Forms.PictureBoxSizeMode.StretchImage;
}
}
catch(Exceptionex)
{
MessageBox.Show("捕捉屏幕出错!
server"+ex.Message);
}
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(button1.Text=="捕获屏幕")
{
for(intx=0;x<4;x++)
this.pbs[x].Visible=true;
button1.Text="停止捕获";
}
else
{
for(intx=0;x<4;x++)
this.pbs[x].Visible=false;
button1.Text="捕获屏幕";
}
}
//广播屏幕
publicvoidoutscreen(Objectindex)
{
intiii=(int)index;
socket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
socket.Bind(newIPEndPoint(IPAddress.Any,this.sendports[iii]));
socket.Listen(10);
try
{
parb[i++]=socket.Accept();
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
if(button2.Text=="广播屏幕")
{
this.timer1.Enabled=true;
button2.Text="停止广播";
}
else
{
this.timer1.Enabled=false;
button2.Text="广播屏幕";
}
}
//发送文件
publicvoidsendfile(Objectindex)
{
intii=(int)index;
para=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
para.Bind(newIPEndPoint(IPAddress.Any,this.fileports[ii]));
para.Listen(10);
try
{
parc[j++]=para.Accept();
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
privatebyte[]ReceiveVarData(Socketds)
{
inttotal=0;
intrecv;
byte[]datasize=newbyte[4];
recv=ds.Receive(datasize,0,4,SocketFlags.None);
intsize=BitConverter.ToInt32(datasize,0);
intdataleft=size;
byte[]data=newbyte[size];
while(total { recv=ds.Receive(data,total,dataleft,SocketFlags.None); if(recv==0) { data=null; break; } total+=recv; dataleft-=recv; } returndata; } privatevoidSendVarData(Socketpard,byte[]b) { inttotal=0; intsize=b.Length; intdataleft=size; intsent; byte[]datasize= newbyte[4]; try { datasize=BitConverter.GetBytes(size); sent=pard.Send(datasize); while(total { sent=pard.Send(b,total,dataleft,SocketFlags.None); total+=sent; dataleft-=sent; return; } } catch { return; } } privatevoidbutton3_Click(objectsender,EventArgse) { try { OpenFileDialogopenFileDialog1=newOpenFileDialog(); openFileDialog1.Filter="所有文件(*.*)|*.*"; if(openFileDialog1.ShowDialog()==DialogResult.OK) { byte[]aaa=System.Text.Encoding.Unicode.GetBytes("F"+openFileDialog1.SafeFileName); for(intjj=0;jj SendVarData(parc[jj],aaa); MessageBox.Show("准备发送文件: "+openFileDialog1.FileName); FileStreamfs=newFileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read); try { byte[]buffur=newbyte[fs.Length]; fs.Read(buffur,0,(int)fs.Length); for(intjj=0;jj SendVarData(parc[jj],buffur); MessageBox.Show("发送成功! "); } catch(Exceptionex) { MessageBox.Show(ex.ToString()); } finally { if(fs! =null) { fs.Close(); } } } } catch(Exceptionee) { MessageBox.Show(ee.Message); } } privatevoidForm1_Load(objectsender,EventArgse) { } privatevoidtimer1_Tick(objectsender,EventArgse) { capture(); } } } 学生端: usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Windows.Forms; usingSystem.Net; usingSystem.Net.Sockets; usingSystem.IO; usingSystem.Threading; namespacestudent { publicpartialclassForm1: Form { //主要变量定义 Socketss; Socketsocket; Socketso; ThreadrecvThread; Threadrecvfile; //API函数对BitBlt()和CreateDC()的引用 [System.Runtime.InteropServices.DllImport("gdi32.dll")] privatestaticexternIntPtrCreateDC ( stringlpszDriver, stringlpszDevice, stringlpszOutput, IntPtrlpInitData ); [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] privatestaticexternboolBitBlt ( IntPtrhdcDest, intnXDest, intnYDest, intnWidth, intnHeight, IntPtrhdcSrc, intnXSrc, intnYSrc, System.Int32dwRop ); publicForm1() { InitializeComponent(); CheckForIllegalCrossThreadCalls=false; socket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); //教师端IP,端口号为6002显示为窗口 try { socket.Connect(newIPEndPoint(IPAddress.Parse("10.16.18.190"),6002)); MessageBox.Show("ok"); recvThread=newThread(newThreadStart(getRemote)); recvThread.IsBackground=true; recvThread.Start(); } catch(Exceptionex) { MessageBox.Show("捕捉屏幕出错! server"+ex.Message); } so=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); try { so.Connect(newIPEndPoint(IPAddress.Parse("10.16.18.190"),7001)); this.label1.Text+="连接成功! \r\n"; recvfile=newThread(newThreadStart(getfile)); recvfile.IsBackground=true; recvfile.Start(); } catch(Exceptionex) { MessageBox.Show("连接教师端发送文件出错! "+ex.Message); } this.ss=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); try { this.label1.Text+="连接中......\r\n"; this.ss.Connect(newIPEndPoint(IPAddress.Parse("10.16.18.190"),8002)); this.label1.Text+="连接成功! \r\n"; this.timer1.Interval=100; this.timer1.Enabled=true; } catch { this.label1.Text+="连接失败! \r\n"; } } //被捕获窗口设计 publicvoidcapture() { IntPtrdc1=CreateDC("DISPLAY",null,null,(IntPtr)null); Graphicsg1=Graphics.FromHdc(dc1); System.Drawing.ImageMyImage=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,g1); Graphicsg2=Graphics.FromImage(MyImage); IntPtrdc3=g1.GetHdc(); IntPtrdc2=g2.GetHdc(); BitBlt(dc2,0,0,Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,dc3,0,0,133
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 计算机网络 编程 实验 期中 报告