资源简介
vs2005编写,c#使用一个socket同时发送字符串和文件
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//fhf
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace Receiver
{
public partial class FormReceiver : Form
{
Socket socket;
Int32 port = 13000;
string ip = “127.0.0.1“;
Thread mythread;
delegate void setTextbox(string strConn);
setTextbox _setTextbox = null;
public FormReceiver()
{
InitializeComponent();
}
private void FormReceiver_Load(object sender EventArgs e)
{
try
{
_setTextbox = new setTextbox(this.DispText);
mythread = new Thread(new ThreadStart(BeginListen));
mythread.Start();
}
catch
{
socket.Close();
mythread.Abort();
}
}
private void FormReceiver_FormClosing(object sender FormClosingEventArgs e)
{
System.Environment.Exit(0);
}
private void DispText(string str)
{
textBox1.Text = str;
}
private void BeginListen1()
{
IPAddress ipAddress = IPAddress.Parse(ip);
socket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(ipAddress port);
byte[] byteMessage = new byte[1048576];
socket.Bind(iep);
while (true)
{
Socket newSocket = null;
try
{
socket.Listen(10);
newSocket = socket.Accept();
int bytes = 0;
int datanum = 0;
string s = ““;
while (true)
{
bytes = newSocket.Receive(byteMessage byteMessage.Length 0);
datanum = BitConverter.ToInt32(byteMessage 0); //从buffer中的前4个字节读出count
if (bytes <= 0)
break;
s += Encoding.Unicode.GetString(byteMessage4bytes-4);
}
string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime + “ “ + “Message from “ + newSocket.RemoteEndPoint.ToString() + “ :“;
//字符串
msg += s;
//在文本框内显示
this.Invoke(_setTextbox msg);
}
catch(Exception e)
{
newSocket.Close();
MessageBox.Show(e.ToString());
}
}
}
private void Begin
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-08-09 14:26 SocketTest\
目录 0 2012-08-12 00:39 SocketTest\Receiver\
目录 0 2012-08-09 22:34 SocketTest\Receiver\bin\
目录 0 2012-08-12 00:35 SocketTest\Receiver\bin\Debug\
文件 951755 2012-08-12 00:41 SocketTest\Receiver\bin\Debug\1.jpg
文件 24576 2012-08-12 00:39 SocketTest\Receiver\bin\Debug\Receiver.exe
文件 28160 2012-08-12 00:39 SocketTest\Receiver\bin\Debug\Receiver.pdb
文件 5632 2005-11-11 22:25 SocketTest\Receiver\bin\Debug\Receiver.vshost.exe
文件 7396 2012-08-12 00:39 SocketTest\Receiver\FormReceiver.cs
文件 2941 2012-08-12 00:38 SocketTest\Receiver\FormReceiver.Designer.cs
文件 5814 2012-08-12 00:38 SocketTest\Receiver\FormReceiver.resx
目录 0 2012-08-09 22:36 SocketTest\Receiver\obj\
目录 0 2012-08-12 00:39 SocketTest\Receiver\obj\Debug\
文件 849 2012-08-12 00:38 SocketTest\Receiver\obj\Debug\Receiver.csproj.GenerateResource.Cache
文件 24576 2012-08-12 00:39 SocketTest\Receiver\obj\Debug\Receiver.exe
文件 180 2012-08-12 00:38 SocketTest\Receiver\obj\Debug\Receiver.FormReceiver.resources
文件 28160 2012-08-12 00:39 SocketTest\Receiver\obj\Debug\Receiver.pdb
文件 180 2012-08-09 22:36 SocketTest\Receiver\obj\Debug\Receiver.Properties.Resources.resources
目录 0 2012-08-09 22:34 SocketTest\Receiver\obj\Debug\Refactor\
目录 0 2012-08-10 00:37 SocketTest\Receiver\obj\Debug\TempPE\
文件 4608 2012-08-10 00:37 SocketTest\Receiver\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
文件 554 2012-08-12 00:41 SocketTest\Receiver\obj\Receiver.csproj.FileListAbsolute.txt
文件 474 2012-08-09 22:34 SocketTest\Receiver\Program.cs
目录 0 2012-08-09 14:26 SocketTest\Receiver\Properties\
文件 1182 2012-08-09 14:26 SocketTest\Receiver\Properties\AssemblyInfo.cs
文件 2840 2012-08-10 00:37 SocketTest\Receiver\Properties\Resources.Designer.cs
文件 5612 2012-08-09 14:26 SocketTest\Receiver\Properties\Resources.resx
文件 1106 2012-08-10 00:37 SocketTest\Receiver\Properties\Settings.Designer.cs
文件 249 2012-08-09 14:26 SocketTest\Receiver\Properties\Settings.settings
文件 3295 2012-08-10 00:54 SocketTest\Receiver\Receiver.csproj
目录 0 2012-08-12 00:41 SocketTest\Sender\
............此处省略31个文件信息
相关资源
- C# 调用 FastReport 报表的源代码
- 国密签名验签源码C#.rar
- 语音录音、合成、识别 C#科大讯飞源
- VisionPro控件的使用 C# 开发篇.pdf
- C#的DotNetBar2.dll全部
- C#实现的比较牛逼的语法高亮着色编辑
- 基于C#和AE的二次开发
- BleSolution.zip
- 仓库管理系统/课程设计/ASP.NET/C#
- 药店管理系统 B/S模式 asp.net(C#)+
- C#接科大讯飞在线语音合成Windows
- C#房屋中介管理系统
- wpf 3D旋转地球
- asp.net 教材管理系统 代码+论文
- winform导航菜单,多窗口tab 源码程序
- 鲜花预定系统 C# asp.net
- C#-图书管理系统带数据库
- C#项目之在线聊天系统含源码
- 点和线多边形缓冲区 c#算法
- 界面化的N皇后教学软件
- .obj模型读取显示 c#版
- c#车牌识别系统完整实现
- 电子秤称重仪表串口通讯c#程序
- 数据结构C#语言描述高清版
- C#窗体图书管理系统
- C#实现的自行车出租管理有数据库
- C# winform开发的skyline动画文字及图片
- 智能评教系统(C#_ASP.NET)
- 房屋中介管理系统C#+MSQL无错版+使用文
- c#图书管理系统源程序
评论
共有 条评论