|
楼主 |
发表于 2013-4-9 14:02:16
|
显示全部楼层
[code=csharp]现在可以了 可是发一次就断开连接了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Data.SqlClient;
using System.Threading;
using System.Collections;
namespace Aserver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TextBox.CheckForIllegalCrossThreadCalls = false;
}
Thread threadWatch = null; //负责监听客户端的线程
Socket socketWatch = null; //负责监听客户端的套接字
Socket socConnection = null;
string filePath = null;
string fileName = null;
private void Form1_Load(object sender, EventArgs e)
{
//定义一个套接字用于监听客户端发来的信息 包含3个参数(IP4寻址协议,流式连接,TCP协议)
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//服务端发送信息 需要1个IP地址和端口号
// System.Net.IPAddress addr;
// 获得本机局域网IP地址
IPAddress ipaddress = new System.Net.IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
// IPAddress ipaddress = IPAddress.Parse(txtIP.Text.Trim()); //获取文本框输入的IP地址
//将IP地址和端口号绑定到网络节点endpoint上
IPEndPoint endpoint = new IPEndPoint(ipaddress, 2112); //获取文本框上输入的端口号
//监听绑定的网络节点
socketWatch.Bind(endpoint);
//将套接字的监听队列长度限制为20
socketWatch.Listen(20);
//创建一个监听线程
threadWatch = new Thread(WatchConnecting);
//将窗体线程设置为与后台同步
threadWatch.IsBackground = true;
//启动线程
threadWatch.Start();
//启动线程后 txtMsg文本框显示相应提示
txtMsg.AppendText("开始监听客户端传来的信息!" + "\r\n");
}
private void WatchConnecting()
{
while (true) //持续不断监听客户端发来的请求
{
try
{
socConnection = socketWatch.Accept();
}
catch (Exception ex)
{
txtMsg.AppendText(ex.Message); //提示套接字监听异常
break;
}
txtMsg.AppendText("客户端连接成功,两端可以开始通信了...\r\n");
//创建通信线程
ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg);
Thread thr = new Thread(pts);
thr.IsBackground = true;
//启动线程
thr.Start(socConnection);
}
}
private void btnSendImages_Click(object sender, EventArgs e)
{
}
//byte a[6]={0x7E,0x7E,0x00,0x01,0x7F,0x7F};
private void ServerRecMsg(object socketClientPara)
{
Socket socketServer = socketClientPara as Socket;
while (true)
{
//创建一个内存缓冲区 其大小为1024*1024字节 即1M
byte[] arrServerRecMsg = new byte[1024 * 1024 * 10];
//将接收到的信息存入到内存缓冲区,并返回其字节数组的长度
//int length = socketServer.Receive(arrServerRecMsg);
}
}
private DateTime GetCurrentTime()
{
DateTime currentTime = new DateTime();
currentTime = DateTime.Now;
return currentTime;
}
private void btnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofDialog = new OpenFileDialog();
if (ofDialog.ShowDialog(this) == DialogResult.OK)
{
fileName = ofDialog.SafeFileName;
textFileName.Text = fileName;
filePath = ofDialog.FileName;
}
}
private void SendFile(string fileFullPath)
{
string relativePath = @"d:\a\点菜.bmp";
OpenFileDialog ofDialog = new OpenFileDialog();
ofDialog.FileName = relativePath;
fileFullPath = ofDialog.FileName;
if (fileFullPath == null)
{
MessageBox.Show("请选择要发送的文件");
return;
}
else if (fileFullPath != null)
{
//string path = Encoding.UTF8.GetString(arrClientFile, 0, i).Trim();
FileStream fs = new FileStream(fileFullPath, FileMode.Open);
int PacketSize = 1024;
int PacketCount = (int)(fs.Length / ((long)PacketSize));
int LastDataPacket = (int)(fs.Length- ((long)(PacketSize * PacketCount)));
byte[] data = new byte[PacketSize];
for (int x = 0; x < PacketCount; x++)
{
fs.Read(data, 0, data.Length);
CommonModule.SendVarData(socConnection, data);
}
if (LastDataPacket != 0)
{
data = new byte[LastDataPacket];
fs.Read(data, 0, data.Length);
CommonModule.SendVarData(socConnection, data);
}
socConnection.Close();
fs.Close();
}
}
}
}
}[/code] |
|