.Net套接字问题 [英] .Net Socket Questions

查看:76
本文介绍了.Net套接字问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我试图使用.Net套接字创建Server Client应用程序

使用BeginXXX和EndXXX或SocketAsyncEventArgs

服务器仅将数据推送到客户端

我使用Socket类创建客户端和服务器,但是我都工作正常,但是有一个问题
快速发送时数据不一致


当我发送1000条消息时,某些消息可能会丢失或同时到达

感谢

hi
i trying To Creat Server Client Application Using .Net Sockets

With The BeginXXX & EndXXX Or SocketAsyncEventArgs

The Server Only Push The data To Clients

i Used The Socket Class To Create Client & Server But I All Worked Fine But I Have One Problem
the Data Is Confilect When Sending Fast

Ex
When I Send 1000 Message Some Message may Be lost Or Arrived in The Same Time

Thank

推荐答案

Server.cs

使用系统;
使用System.Collections.Generic;
使用System.Text;
使用System.Net;
使用System.IO;
使用System.Threading;
使用System.Net.Sockets;

命名空间ConsoleApplication1
{
类服务器
{
公共无效聊天(对象o)
{
TcpClient srv =(TcpClient)o;
StreamReader读取;
StreamWriter写入;
字符串msg;
read = new StreamReader(srv.GetStream());
写入=新的StreamWriter(srv.GetStream());

而(true)
{

msg = read.ReadLine();
Console.WriteLine(来自客户端{0}:{1}",Thread.CurrentThread.Name,msg);
write.WriteLine(msg);
write.Flush();
如果(msg.Equals("Exit"))
休息;
}
Console.WriteLine(客户端{0}已断开连接....",Thread.CurrentThread.Name,msg);
read.Close();
write.Close();
srv.Close();
}
}
公共课程测试
{
静态void Main(string [] args)
{
//IPAddress ip = new IPAddress();
TcpListener列表=新的TcpListener(IPAddress.Loopback,9999);
int count = 0;
而(true)
{
list.Start();
Console.WriteLine(服务器就绪....正在等待客户端!");
TcpClient srv = list.AcceptTcpClient();
Console.WriteLine(已连接客户端{0}",++ count);
服务器s = new Server();
ParameterizedThreadStart del =新的ParameterizedThreadStart(s.Chat);
线程t =新线程(del);
t.Name =" + count;
t.Start(srv);
}
}
}
}

Client.cs

使用系统;
使用System.Collections.Generic;
使用System.Text;
使用System.IO;
使用System.Net;
使用System.Net.Sockets;

命名空间ConsoleApplication11
{
客户端类
{
公共静态无效的Main(String [] arg)
{
//IPEndPoint ip =新的IPEndPoint(
//IPAddress ip = new IPAddress();
TcpClient cl =新的TcpClient("localhost",9999);
字符串msg;
StreamReader读取;
StreamWriter写入;
read = new StreamReader(cl.GetStream());
写入=新的StreamWriter(cl.GetStream());
而(true)
{
msg = Console.ReadLine();
write.WriteLine(msg);
write.Flush();
msg = read.ReadLine();
Console.WriteLine(msg);
如果(msg.Equals("Exit"))
休息;
}
read.Close();
write.Close();
cl.Close();
}
}
}
Server.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using System.Net.Sockets;

namespace ConsoleApplication1
{
class Server
{
public void Chat(object o)
{
TcpClient srv = (TcpClient)o;
StreamReader read;
StreamWriter write;
String msg;
read = new StreamReader(srv.GetStream());
write = new StreamWriter(srv.GetStream());

while (true)
{

msg = read.ReadLine();
Console.WriteLine("From Client {0}:{1}",Thread.CurrentThread.Name,msg);
write.WriteLine(msg);
write.Flush();
if (msg.Equals("Exit"))
break;
}
Console.WriteLine("Client {0} Disconnected....", Thread.CurrentThread.Name, msg);
read.Close();
write.Close();
srv.Close();
}
}
public class Test
{
static void Main(string[] args)
{
// IPAddress ip = new IPAddress();
TcpListener list = new TcpListener(IPAddress.Loopback, 9999);
int count = 0;
while (true)
{
list.Start();
Console.WriteLine("Server Ready....Waiting for Clients!");
TcpClient srv = list.AcceptTcpClient();
Console.WriteLine("Client {0} connected", ++count);
Server s = new Server();
ParameterizedThreadStart del = new ParameterizedThreadStart(s.Chat);
Thread t = new Thread(del);
t.Name = "" + count;
t.Start(srv);
}
}
}
}

Client.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace ConsoleApplication11
{
class Client
{
public static void Main(String[] arg)
{
//IPEndPoint ip = new IPEndPoint(
// IPAddress ip = new IPAddress();
TcpClient cl = new TcpClient("localhost", 9999);
String msg;
StreamReader read;
StreamWriter write;
read = new StreamReader(cl.GetStream());
write = new StreamWriter(cl.GetStream());
while (true)
{
msg = Console.ReadLine();
write.WriteLine(msg);
write.Flush();
msg=read.ReadLine();
Console.WriteLine(msg);
if (msg.Equals("Exit"))
break;
}
read.Close();
write.Close();
cl.Close();
}
}
}


这篇关于.Net套接字问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆