没有合适的资源?快使用搜索试试~ 我知道了~
socket 传文件 客户端,服务器socket 传文件 客户端,服务器
资源推荐
资源详情
资源评论
C#SocketSC结构文件传输 收藏
下面是服务器端的代码,里面注释都很清楚我就不解释了
Serve form 上有一个btnListen的button,一个表连接状态的:Connection State的label,还有一个表连接状态的txtBox:txtConState
view plaincopy to clipboardprint?
・・・・・・・・・10・・・・・・・・20・・・・・・・・30・・・・・・・・40・・・・・・・・50・・・・・・・・60・・・・・・・・70・・・・・・・・80・・・・・・・・90・・・・・・・・100・・・・・・・110・・・・・・・120・・・・・・・130・・・・・・・140・・・・・・・150
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace FileSendServer
{
public partial class FileSendServer : Form
{
public FileSendServer()
{
InitializeComponent();
}
private void btnListen_Click(object sender, EventArgs e)
{
下面是服务器端的代码,里面注释都很清楚我就不解释了
Serve form 上有一个btnListen的button,一个表连接状态的:Connection State的label,还有一个表连接状态的txtBox:txtConState
view plaincopy to clipboardprint?
・・・・・・・・・10・・・・・・・・20・・・・・・・・30・・・・・・・・40・・・・・・・・50・・・・・・・・60・・・・・・・・70・・・・・・・・80・・・・・・・・90・・・・・・・・100・・・・・・・110・・・・・・・120・・・・・・・130・・・・・・・140・・・・・・・150
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace FileSendServer
{
public partial class FileSendServer : Form
{
public FileSendServer()
{
InitializeComponent();
}
private void btnListen_Click(object sender, EventArgs e)
{
object obj = new string[] { "172.25.73.158", "D:\\test.txt" };
//to parse the obj to IP and filename
string[] str = (string[])(obj);
string clientIP = str[0];
string filename = str[1];
try
{
//initialize a Socket Instance;
Socket listenSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress hostIP = Dns.GetHostEntry(clientIP).AddressList[0]; //将主机名或 IP 地址解析为 IPHostEntry 实例。
//将网络端点表示为 IP 地址和端口号。
IPEndPoint ep = new IPEndPoint(hostIP, 11000); //用指定的地址和端口号初始化 IPEndPoint 类的新实例。
////取得主机IP
//IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
//IPAddress ipAddr = ipHost.AddressList[0];
//IPEndPoint ep1 = new IPEndPoint(ipAddr, 11000); //用指定的地址和端口号初始化 IPEndPoint 类的新实例。
//listenSocket
listenSocket.Bind(ep); //要与 Socket 关联的本地 EndPoint。
listenSocket.Listen(5); //将 Socket 置于侦听状态,参数backlog为挂起连接队列的最大长度
//accept the call
Socket mySoket = listenSocket.Accept(); //为新建连接创建新的 Socket。
txtConState.Text = "已建立联接!";
//define a buff
Byte[] buff = new Byte[256];
//to parse the obj to IP and filename
string[] str = (string[])(obj);
string clientIP = str[0];
string filename = str[1];
try
{
//initialize a Socket Instance;
Socket listenSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress hostIP = Dns.GetHostEntry(clientIP).AddressList[0]; //将主机名或 IP 地址解析为 IPHostEntry 实例。
//将网络端点表示为 IP 地址和端口号。
IPEndPoint ep = new IPEndPoint(hostIP, 11000); //用指定的地址和端口号初始化 IPEndPoint 类的新实例。
////取得主机IP
//IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
//IPAddress ipAddr = ipHost.AddressList[0];
//IPEndPoint ep1 = new IPEndPoint(ipAddr, 11000); //用指定的地址和端口号初始化 IPEndPoint 类的新实例。
//listenSocket
listenSocket.Bind(ep); //要与 Socket 关联的本地 EndPoint。
listenSocket.Listen(5); //将 Socket 置于侦听状态,参数backlog为挂起连接队列的最大长度
//accept the call
Socket mySoket = listenSocket.Accept(); //为新建连接创建新的 Socket。
txtConState.Text = "已建立联接!";
//define a buff
Byte[] buff = new Byte[256];
剩余8页未读,继续阅读
资源评论
mozhuluo
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功