首页 新闻 会员 周边 捐助

C#Socket问题

0
悬赏园豆:20 [已关闭问题] 关闭于 2015-02-11 23:08

我想用FileStream和Socket知识写一个文件收发Server和Client

Server代码:

class Program
{

static void Main(string[] args)
{
//创建一个新的Socket
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse("127.0.0.1");
socket.Bind(new IPEndPoint(ip, 80));
//启动监听,并且设置一个最大的队列长度
socket.Listen(5);
//开始接受客户端连接请求
socket.BeginAccept(new AsyncCallback(ClientAccepted), socket);

Console.WriteLine("Server is ready!");
Console.Read();
}

public static void ClientAccepted(IAsyncResult ar)
{
string fileName = "D:\\downloadable.txt";

var socket = ar.AsyncState as Socket;
//客户端的Socket实例
var client = socket.EndAccept(ar);

//检测客户端Socket状态
if (client.Connected)
{
try
{
client.Send(Encoding.Unicode.GetBytes("connecting successfully!\n"));
client.Send(Encoding.Unicode.GetBytes("Downloading..."));
client.BeginSendFile(fileName, new AsyncCallback(FileSendCallback), client);
//client.Shutdown(SocketShutdown.Both);
//client.Close();
}
catch (SocketException ex)
{
Console.WriteLine(ex.Message);
}
}
else
{
Console.WriteLine("Client is disconnected!");
}
//准备接受下一个客户端请求
socket.BeginAccept(new AsyncCallback(ClientAccepted), socket);
}
//发送
private static void FileSendCallback(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
//结束
client.EndSendFile(ar);

}
}

 

Client端代码:

class Program
{
static byte[] buffer = new byte[1024];
static void Main(string[] args)
{
//创建一个Socket
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//连接到端口,”localhost“ == 127.0.0.1
socket.Connect("localhost", 80);

//接收消息
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback((ar) =>
{
var length = socket.EndReceive(ar);
//读取出来消息内容
var message = Encoding.Unicode.GetString(buffer, 0, length);
//显示消息
Console.WriteLine(message);
}), null);
Console.WriteLine("Connecting...");
//开始从连接的 Socket 中异步接收数据。
socket.BeginReceive(buffer, 0, buffer.Length,
SocketFlags.None, new AsyncCallback(Receive), socket);

Console.Read();
}

 

public static void Receive(IAsyncResult ar)
{
try
{
//服务器的Socket实例
var socket = ar.AsyncState as Socket;
var length = socket.EndReceive(ar);
var message = Encoding.Unicode.GetString(buffer, 0, length);
//显示消息
Console.WriteLine(message);
//接收路径
string path = "F:\\downloadable.txt";
//创建流
int bytesRead;
int totalBytes = 0;
//NetworkStream ns = new NetworkStream(socket);
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
do
{
bytesRead = socket.Receive(buffer);
fs.Write(buffer, 0, bytesRead);
totalBytes += bytesRead;
Console.WriteLine("Download successfully! Receive {0} bytes!", totalBytes);
} while (bytesRead > 0);
fs.Close();
//socket.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

}
}

 

运行的时候,服务器发送文件,里面有内容,但是Client只是创建了文件,里面没有内容....

而且Client窗口显示的东西也有问题...

Yeeyao的主页 Yeeyao | 初学一级 | 园豆:140
提问于:2015-02-04 20:45
< >
分享
所有回答(2)
0

你还没有开始接收数据就一个EndReceive(),当然接收不到数据!

上帝之城 | 园豆:2549 (老鸟四级) | 2015-02-05 12:54
0

按你代码,我只修改了IP(192.168.1.69)和端口(8082)

一切正常

Yu | 园豆:12990 (专家六级) | 2015-02-05 17:24

谢谢,我试试

支持(0) 反对(0) Yeeyao | 园豆:140 (初学一级) | 2015-02-07 15:35

正常是收到全部文件数据?

支持(0) 反对(0) Yeeyao | 园豆:140 (初学一级) | 2015-02-07 15:36

@Yeeyao: 嗯

支持(0) 反对(0) Yu | 园豆:12990 (专家六级) | 2015-02-07 15:49

@Yeeyao: client 多下几次就不行了

支持(0) 反对(0) Yu | 园豆:12990 (专家六级) | 2015-02-07 16:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册