MSDN上有示例,思路是:
服务器:TcpListener.Start();
TcpClient s=TcpLitener.AcceptSocket();客户端:TcpClient.Connect();//connect to the server然后用 NetworkStream 进行传输。
服务器:TcpListener.Start();
TcpClient s=TcpLitener.AcceptSocket();客户端:TcpClient.Connect();//connect to the server然后用 NetworkStream 进行传输。
static void Main(string[] args)
{
IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
System.Net.Sockets.TcpListener listener=new System.Net.Sockets.TcpListener(ipAddress,1500);
listener.Start(); Byte[] bytes = new Byte[256];
String data = null; while(true)
{
Console.Write("Waiting for a connection... ");
TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("Connected!");
NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
Console.WriteLine(String.Format("Received: {0}", data));
//
// data = data.ToUpper();
//
// Byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);
//
// // Send back a response.
// stream.Write(msg, 0, msg.Length);
// Console.WriteLine(String.Format("Sent: {0}", data));
}
// Shutdown and end connection
client.Close(); }
static void Main(string[] args)
{ Byte[] bytes = new Byte[256];
String data = null; TcpClient client=new TcpClient();
client.Connect("localhost",1500);
NetworkStream stream = client.GetStream();
string data2="中文";
Byte[] msg = System.Text.Encoding.UTF8.GetBytes(data2);
stream.Write(msg,0,msg.Length); int i; // Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
Console.WriteLine(String.Format("Received: {0}", data));
}
client.Close();
}
回复人: taojm(桃子) ( ) 信誉:100 2003-12-7 10:13:50 得分:5
给你一段例子代码,上传图片进入数据库的,不懂可以继续提问:
private void UploadImage_Click(object sender, System.EventArgs e)
{
/// <summary>
/// ContentType holds the content type of the file that was added to the ImageToUpload
/// control, and HTML input control. </summary>
string ContentType = ImageToUpload.PostedFile.ContentType;
/// <summary>
/// Length holds the size of the file that was added to the ImageToUpload control </summary>
int Length = System.Convert.ToInt32(ImageToUpload.PostedFile.InputStream.Length); /// <summary>
/// Content will hold the image. It is a byte array of size Length </summary>
byte[] Content = new byte[Length]; /// <summary>
/// The Read method is used to read the file from the ImageToUpload control </summary>
ImageToUpload.PostedFile.InputStream.Read(Content,0,Length); /// <summary>
/// Open a connection to the SQL Server </summary>
SqlConnection Connection = new SqlConnection("server=localhost;uid=sa;pwd=;database=ImageUpload");
/// <summary>
/// The SqlCommand will be used to insert the image into the Images table </summary>
SqlCommand Command = new SqlCommand("INSERT Into Images(Description, ImageFile, ImageSize, ImageType) Values(@Description, @ImageFile, @ImageSize, @ImageType)", Connection); /// <summary>
/// The Description parameter is used to add the image file description to the database
SqlParameter imageDescriptionParameter = new SqlParameter("@Description", SqlDbType.NVarChar);
imageDescriptionParameter.Value = imageDescription.Text;
Command.Parameters.Add(imageDescriptionParameter); /// <summary>
/// The ImageFile parameter is used to add the image file to the database
SqlParameter imageFileParameter = new SqlParameter("@ImageFile", SqlDbType.Image);
imageFileParameter.Value = Content;
Command.Parameters.Add(imageFileParameter); /// <summary>
/// The ImageSize parameter is used to add the image file size to the database
SqlParameter imageSizeParameter = new SqlParameter("@ImageSize", SqlDbType.Int);
imageSizeParameter.Value = Length;
Command.Parameters.Add(imageSizeParameter); /// <summary>
/// The ImageType parameter is used to add the image file type to the database
SqlParameter imageTypeParameter = new SqlParameter("@ImageType", SqlDbType.NVarChar);
imageTypeParameter.Value = ContentType;
Command.Parameters.Add(imageTypeParameter); /// <summary>
/// Open the connection in order to retrieve the record </summary>
Connection.Open();
/// <summary>
/// The SQL statement is executed. ExecuteNonQuery is used since no records
/// will be returned. </summary>
Command.ExecuteNonQuery();
/// <summary>
/// The connection is closed </summary>
Connection.Close();
}
Top
如何把 图片转化成流格式?
to : taojm(桃子)
一般都不是把文件以二进制格式存到数据库而是存放到服务器端的文件夹中,代码我星期一发
放一个form runat=server, file runat=server,botton runat=server
在form的load中写上 if (!f1.PostedFile.FileName.Equals(""))
{
string[] spf1=f1.PostedFile.FileName.Split('\\');
f1.PostedFile.SaveAs("d:\\"+spf1[spf1.Length-1].ToString());
}