附程序:
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;namespace socket开发指南版客户端
{
public partial class RemoteFileClient : Form
{
protected string hostIp;
protected int hostPort;
protected Socket ClientSocket; public RemoteFileClient()
{
InitializeComponent();
} public bool GetFile(string fileNameToGet)
{
//因为socket只能发送字节,所以有如下的转换
byte[] senddata = new byte[1024];
senddata = Encoding.Default.GetBytes(fileNameToGet);
//发送文件名
try
{
ClientSocket.Send(senddata, senddata.Length, SocketFlags.None);
}
catch (Exception ex)
{
MessageBox.Show("Error21:" + ex.Message);
return false;
}
NetworkStream ns = new NetworkStream(ClientSocket);
byte[] buffer = new byte[1024];
int readBytes = 0;
try
{
readBytes = ns.Read(buffer, 0, 1024);
}
catch (Exception ex)
{
MessageBox.Show("Error22:" + ex.Message);
return false;
} string request = Encoding.Default.GetString(buffer,0, readBytes);
if (request.StartsWith("N")) return false; //接收文件
int pos = fileNameToGet.LastIndexOf(@"\") + 1;
int length = fileNameToGet.Length - pos;
this.saveFileDialog1.FileName = fileNameToGet.Substring(pos,length);
if (this.saveFileDialog1.ShowDialog() != DialogResult.OK)
return false;
string path = this.saveFileDialog1.FileName;
MessageBox.Show(path);
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
int count;
byte[] receivedata = new byte[4096];
try
{
while ((count = ClientSocket.Receive(receivedata ,4096, SocketFlags.None)) != 0)
{
// MessageBox.Show(count.ToString());
bw.Write(receivedata, 0, count);
}
MessageBox.Show("state:接收成功");
this.label1.Text = "state:接收成功";
}
catch (Exception ex)
{
MessageBox.Show("Error23:" + ex.Message);
return false;
}
bw.Close();
fs.Close();
return true;
} public bool SetUpConnect()
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
return false;
this.hostIp = textBox2.Text.Trim().ToString();
this.hostPort = int.Parse(textBox3.Text.Trim().ToString());
try
{
IPAddress Ip = IPAddress.Parse(hostIp);
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ClientSocket.Connect(Ip, hostPort);
}
catch (Exception ex)
{
MessageBox.Show("ErrorConnect:" + ex.Message);
return false; ;
}
return true;
} public void TeardownConnect()
{
try
{
//ClientSocket.Shutdown();//禁用socket
ClientSocket.Close();//关闭socket
}
catch (Exception ex)
{
MessageBox.Show("ErrorDis:" + ex.Message);
}
} private void button3_Click(object sender, EventArgs e)
{
if (SetUpConnect())
{
this.label2.Text = "State:连接成功";
bool flag=GetFile(textBox1.Text.Trim().ToString());
TeardownConnect();
this.label2.Text = "State:连接断开";
}
} private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();
textBox1.Text = this.openFileDialog1.FileName;
}
}
}
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;namespace socket开发指南版客户端
{
public partial class RemoteFileClient : Form
{
protected string hostIp;
protected int hostPort;
protected Socket ClientSocket; public RemoteFileClient()
{
InitializeComponent();
} public bool GetFile(string fileNameToGet)
{
//因为socket只能发送字节,所以有如下的转换
byte[] senddata = new byte[1024];
senddata = Encoding.Default.GetBytes(fileNameToGet);
//发送文件名
try
{
ClientSocket.Send(senddata, senddata.Length, SocketFlags.None);
}
catch (Exception ex)
{
MessageBox.Show("Error21:" + ex.Message);
return false;
}
NetworkStream ns = new NetworkStream(ClientSocket);
byte[] buffer = new byte[1024];
int readBytes = 0;
try
{
readBytes = ns.Read(buffer, 0, 1024);
}
catch (Exception ex)
{
MessageBox.Show("Error22:" + ex.Message);
return false;
} string request = Encoding.Default.GetString(buffer,0, readBytes);
if (request.StartsWith("N")) return false; //接收文件
int pos = fileNameToGet.LastIndexOf(@"\") + 1;
int length = fileNameToGet.Length - pos;
this.saveFileDialog1.FileName = fileNameToGet.Substring(pos,length);
if (this.saveFileDialog1.ShowDialog() != DialogResult.OK)
return false;
string path = this.saveFileDialog1.FileName;
MessageBox.Show(path);
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
int count;
byte[] receivedata = new byte[4096];
try
{
while ((count = ClientSocket.Receive(receivedata ,4096, SocketFlags.None)) != 0)
{
// MessageBox.Show(count.ToString());
bw.Write(receivedata, 0, count);
}
MessageBox.Show("state:接收成功");
this.label1.Text = "state:接收成功";
}
catch (Exception ex)
{
MessageBox.Show("Error23:" + ex.Message);
return false;
}
bw.Close();
fs.Close();
return true;
} public bool SetUpConnect()
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
return false;
this.hostIp = textBox2.Text.Trim().ToString();
this.hostPort = int.Parse(textBox3.Text.Trim().ToString());
try
{
IPAddress Ip = IPAddress.Parse(hostIp);
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ClientSocket.Connect(Ip, hostPort);
}
catch (Exception ex)
{
MessageBox.Show("ErrorConnect:" + ex.Message);
return false; ;
}
return true;
} public void TeardownConnect()
{
try
{
//ClientSocket.Shutdown();//禁用socket
ClientSocket.Close();//关闭socket
}
catch (Exception ex)
{
MessageBox.Show("ErrorDis:" + ex.Message);
}
} private void button3_Click(object sender, EventArgs e)
{
if (SetUpConnect())
{
this.label2.Text = "State:连接成功";
bool flag=GetFile(textBox1.Text.Trim().ToString());
TeardownConnect();
this.label2.Text = "State:连接断开";
}
} private void button1_Click(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();
textBox1.Text = this.openFileDialog1.FileName;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货