发送端
using Microsoft.Win32;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.NetworkInformation;
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
public string StrIp = "";
System.Net.Sockets.Socket sendsocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
// *********************************************************************************************最小化托盘
this.Hide();
this.ShowInTaskbar = false;
string starupPath = Application.ExecutablePath;
RegistryKey loca = Registry.LocalMachine;
RegistryKey run = loca.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
try
{
run.SetValue("ss", starupPath);
//MessageBox.Show("开机自动启动设置成功!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
loca.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// ****************************************************************************************最小化托盘
//实例化socket
System.Net.IPEndPoint ipendpiont = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(StrIp.Replace("'", "''").Trim()),8080);
try
{
sendsocket.Connect(ipendpiont);
}
catch (Exception se)
{
// MessageBox.Show("连接错误" + se.Message, "提示信息", MessageBoxButtons.RetryCancel, MessageBoxIcon.Information);
sendsocket.Close();
return;
}
try
{
//********************开始使用socket发送文件********************//
//建立终结点
System.IO.FileStream fs = new System.IO.FileStream("d://新建文件夹//a.jpg", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read);
byte[] fssize = new byte[fs.Length];
System.IO.BinaryReader strread = new System.IO.BinaryReader(fs);
strread.Read(fssize, 0, fssize.Length - 1);
sendsocket.Send(fssize);
fs.Close();
sendsocket.Shutdown(System.Net.Sockets.SocketShutdown.Send);
sendsocket.Close();
//***********************************************************************************************************end*******************************************************//
}
catch (Exception ex)
{
string s = ex.ToString();
return;
}
finally
{
//强制关闭发送连接
sendsocket.Close();
}
#region 时钟连接
private void timer1_Tick(object sender, EventArgs e)
{
if (sendsocket.Connected)
{
this.label1.Text = "连接成功";
}
else
{
return;
}
}
#endregion
接收端
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Drawing.Imaging;
using System.Threading;
using Microsoft.Win32;
using System.Data.SqlClient;
public partial class Admin_FilmPic : System.Web.UI.Page
{
System.Net.Sockets.Socket receivesocket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
System.Net.IPEndPoint hostipendpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 8080);
#region 初始化
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
#endregion
#region 监听和连接
//监听和连接
protected void Button2_Click(object sender, EventArgs e)
{
try
{
//设置接收数据缓冲区的大小
byte[] b = new byte[1024 * 4]; //1024* 4
receivesocket.Bind(hostipendpoint);
//监听
receivesocket.Listen(2);
this.Label1.Text = "已经开始监听";
System.Net.Sockets.Socket hostsocket = receivesocket.Accept();
//如何确定该数组大小
System.IO.MemoryStream fs = new System.IO.MemoryStream();
int got = 0;
int datalength = 0;
while (true)
{
got = hostsocket.Receive(b);
fs.Write(b, 0, got);
if (got > 0)
datalength = datalength + got;
else
break;
}
Bitmap Img = new Bitmap(fs);
Img.Save(Server.MapPath(Request.ApplicationPath) + "//pic//a.jpg", ImageFormat.Jpeg);
string aa = Server.MapPath(Request.ApplicationPath) + "//pic//a.jpg";
this.Image1.ImageUrl = aa;
this.Image1.Width = 600;
this.Image1.Height = 400;
//关闭写文件流
fs.Close();
//关闭接收数据的Socket
hostsocket.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
hostsocket.Close();
//关闭发送连接
receivesocket.Close();
// this.Response.Write(" <script language=javascript>alert('传输成功!');</script>");
}
catch (Exception se)
{
receivesocket.Close();
this.Response.Write("连接错误" + se.ToString());
return;
}
finally
{
//强制关闭发送连接
receivesocket.Close();
}
}
#endregion
}