利用TcpClient TcpListener 实现发送图片
點(diǎn)擊此處下載源代碼
客戶端:實(shí)現(xiàn)向服務(wù)器發(fā)送文件
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Net;
?
namespace tcpclient
{
??? class tcpclient
??? {
??????? private static int portNum = 11000;
??????? static void Main(string[] args)
??????? {
????????? ??//第一步:讀取文件
??????????? Console.WriteLine("本程序?qū)崿F(xiàn)向服務(wù)器發(fā)送文件...");
??????????? FileStream fs = File.Open("http://www.cnblogs.com/img.gif", FileMode.Open);
??????????? byte[] buffer = new byte[fs.Length];
??????????? fs.Read(buffer, 0, (int)fs.Length);
??????????? fs.Close();
?
??????????? //第二步:發(fā)送文件
??????????? try
??????????? {
??????????????? TcpClient client = new TcpClient(System.Net.Dns.GetHostName(), portNum);
??????????????? NetworkStream ns = client.GetStream();
??????????????? ns.Write(buffer, 0, buffer.Length);
????? ??????????ns.Close();
??????????????? client.Close();
??????????? }
??????????? catch (Exception e)
??????????? {
??????????????? Console.WriteLine(e.ToString());
??????????? }
??????? }
??? }
}
?
服務(wù)器端:不斷接收客戶端的輸入
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Net;
?
namespace tcpfile
{
??? class tcpserver
??? {
??????? private const int portNum = 11000;
??????? static void Main(string[] args)
??????? {
??????????? bool done = false;
??????????? //TcpListener listener = new TcpListener(portNum); //根據(jù)VS2005 MSDN 此方法已經(jīng)過(guò)時(shí),不再使用
??????????? // IPEndPoint類(lèi)將網(wǎng)絡(luò)標(biāo)識(shí)為IP地址和端口號(hào)
??????????? TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Any, portNum));
??????????? listener.Start();
???????????while (!done)
??????????? {
??????????????? Console.Write("Waiting for connection...");
??????????????? TcpClient client = listener.AcceptTcpClient();
??????????????? Console.WriteLine("Connection accepted.");
??????????????? NetworkStream ns = client.GetStream();
??????????????
??????????????? byte[] tempBuffer = new byte[4096];
??????????????? FileStream fs = File.Open(new Random().Next().ToString() + ".gif", FileMode.CreateNew);
??????????????? int bytesRead = 0;
??????????????? do
?????????? ?????{
??????????????????? bytesRead = ns.Read(tempBuffer, 0, 4096);
??????????????????? fs.Write(tempBuffer, 0, bytesRead);
??????????????? }while (bytesRead > 0);
???????????????
??????????????? fs.Close();
??????????????? client.Close();
????????????? ??Console.WriteLine("file accepted.");
??????????????
??????????? }
??????????? listener.Stop();
??????? }
?
??? }
}
?
/* 如果不是文件的話,可以在內(nèi)存中存儲(chǔ)一個(gè)臨時(shí)內(nèi)存流,以便后續(xù)處理,如:賦給Image控件,壓縮流等。
?*?一種典型的從客戶端讀取流的方法
?? NetworkStream clientStreams = client.GetStream();
?? int bufferSize = 4096;
?? byte[] responseBuffer = new byte[bufferSize];
?? MemoryStream memStream = new MemoryStream();
?? int bytesRead = 0;
?? do
?? {
?????? bytesRead = clientStream.Read(responseBuffer, 0, bufferSize);
?????? memStream.Write(responseBuffer, 0, bytesRead);
??? } while (bytesRead > 0);
*/
?
/*
?* 這種模式,是不被 NetworkStream clientStreams = client.GetStream(); 所支持的
byte[] buffer = new byte[clientStreams.Length];
clientStream.Read(buffer, 0, (int)fs.Length);
*/
?
轉(zhuǎn)載于:https://www.cnblogs.com/qqhfeng/archive/2009/10/08/1578945.html
總結(jié)
以上是生活随笔為你收集整理的利用TcpClient TcpListener 实现发送图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Enterprise Library 4
- 下一篇: 用Windows Live Writer