各位。NET高手:求一TCP/IP通讯解决方案

zhanlinzhangna520 2007-11-02 03:02:14
问题需求如下:
服务器一台,客户端若干个。
现在就是要实现服务器和客户端之间的通讯,服务器端如何实现和各个客户端之间通过TCP/IP通讯,并把各个客户端发送过来的数据存储到数据库中。
我在实现P2P的点对点通讯中已经实现了,但在1:N的通讯中不知道怎么设计(架构),在这样的通讯过程中是不是要考虑并发问题?另外我的客户端不是PC,是硬件终端,如:ARM,电子称之类的东西。请各位高手出招。谢谢!
...全文
268 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
micher_yan 2007-11-08
  • 打赏
  • 举报
回复
不知道 WebServices 或者 Remoting能否解决你的问题
yhy0611 2007-11-08
  • 打赏
  • 举报
回复
我也在找这方面的资料,帮你顶!
yhy0611 2007-11-08
  • 打赏
  • 举报
回复
再顶~!
micher_yan 2007-11-08
  • 打赏
  • 举报
回复
我们现在做的工程是通过Remoting实现的,类 函数都是通过网络传送,习惯以后发现编程效率不错,可以避开网络协议相关的问题,或者根本就不需要去考虑网络协议了,一个客户端触发一个服务器消息,服务器处理完了以后也可以向其他客户端广播,具体的可以查一查相关的资料,网上有很多的
it73202 2007-11-06
  • 打赏
  • 举报
回复
IOCP
wangsaokui 2007-11-06
  • 打赏
  • 举报
回复
关键是每来一个客户端就RunClient开一个线程处理
wangsaokui 2007-11-06
  • 打赏
  • 举报
回复
客户端
using System;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Text;

namespace TCPClientDemo
{
class Program
{
const int ECHO_PORT = 8080;
static void Main(string[] args)
{
Console.Write("Your UserName:");
string userName = Console.ReadLine();
Console.WriteLine("-----Logged in-----");

try
{
//Create a connection with the Chatserver
TcpClient eClient = new TcpClient("127.0.0.1", ECHO_PORT);

//Create the stream classes
StreamReader readerStream = new StreamReader(eClient.GetStream());
NetworkStream writeStream = eClient.GetStream();

string dataToSend;

dataToSend = userName;
dataToSend += "\r\n";

//Send username to the server
byte[] data = Encoding.ASCII.GetBytes(dataToSend);
writeStream.Write(data, 0, data.Length);

while (true)
{
Console.WriteLine(userName + ":");
//Read line from server
dataToSend = Console.ReadLine();
dataToSend += "\r\n";
data = Encoding.ASCII.GetBytes(dataToSend);
writeStream.Write(data, 0, data.Length);
//If QUIT is sent, then quit application
if (dataToSend.IndexOf("QUIT") > -1)
break;

string returnData;

//Receive response from server
returnData = readerStream.ReadLine();
Console.WriteLine("Server: " + returnData);
}

//Close TcpClient
eClient.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
wangsaokui 2007-11-06
  • 打赏
  • 举报
回复
服务器
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace TCPServerDemo
{
class ClientHandler
{
public TcpClient clientSocket;

public void RunClient()
{
//Create the stream classes
Console.WriteLine(clientSocket.Client.AddressFamily.ToString());
StreamReader readerStream = new StreamReader(clientSocket.GetStream());
NetworkStream writestream = clientSocket.GetStream();

string returnData = readerStream.ReadLine();
string userName = returnData;

Console.WriteLine("Welcome " + userName + " to the Server");
while (true)
{
returnData = readerStream.ReadLine();
if (returnData.IndexOf("QUIT") > -1)
{
Console.WriteLine("Bye Bye " + userName);
break;
}

Console.WriteLine(userName + ": " + returnData);
returnData += "\r\n";

byte[] dataWrite = Encoding.ASCII.GetBytes(returnData);
writestream.Write(dataWrite, 0, dataWrite.Length);
}
clientSocket.Close();
}
}
public class EchoServer
{
const int ECHO_PORT = 8080;
public static int nClient = 0;

static void Main(string[] args)
{
try
{
//Bind the Server to local port
TcpListener clientListener = new TcpListener(IPAddress.Parse("127.0.0.1"),ECHO_PORT);

//start to listen
clientListener.Start();

Console.WriteLine("Waiting for connection...");

while (true)
{
//Accept the connection
TcpClient client = clientListener.AcceptTcpClient();
ClientHandler cHandler = new ClientHandler();

//pass value to the ClientHandler object
cHandler.clientSocket = client;
string ipaddressport = client.Client.RemoteEndPoint.ToString();
Console.WriteLine(ipaddressport.Substring(0, ipaddressport.IndexOf(":")) + "--------------"+ipaddressport.Substring(ipaddressport.IndexOf(":")+1), ipaddressport.Length - ipaddressport.IndexOf(":")-1);
//Create a new thread for the client
Thread clientThread = new Thread(new ThreadStart(cHandler.RunClient));
clientThread.Start();
}
clientListener.Stop();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
新月星雪 2007-11-06
  • 打赏
  • 举报
回复
可以不去设置SockerServer的最大连接数吧
wzd24 2007-11-06
  • 打赏
  • 举报
回复
你可以用异步Socket来处理数据收发任务。然后在线程池中处理数据。关于异步Socket你可以看看我的Blog:http://blog.csdn.net/wzd24 or http://wzd24.cnblogs.com
zhanlinzhangna520 2007-11-06
  • 打赏
  • 举报
回复
同意楼上各位的意见,用线程进行监听,如果有连接就开另外一个线程接受数据/发送数据,大家考虑到没有,用这样的方法时要考虑线程个数问题,如果限制了线程数的话,那么有些客户发送过来的数据就存在丢客户的问题,大家你说是不是有这样的问题?
另注:只要发言,大家都有分,不够我再加。呵呵,有现成的例子也可以发送到我信箱里,地址zhanlincity@163.com,感激不尽,谢谢大家!
TonyTonyQ 2007-11-02
  • 打赏
  • 举报
回复
同意 iepshen 的说法,一般都作法都是一个线程监听端口,如果有连接进来,则为每个连接新开一个线程负责收发数据。不过最好对最大线程数进行些限制,以免出现性能问题。
iepshen 2007-11-02
  • 打赏
  • 举报
回复
首先 服务器侦听 ,得到一个连接之后, 开始一个线程用于时刻接受数据;

并将此连接存放在一个HashTable中

发送数据时候根据HashTable中的key得到连接,向指定的Client发送数据。

大概就是这样的 。有机会可以加MSN:iepshen@hotmail.com 讨论
windily 2007-11-02
  • 打赏
  • 举报
回复
等待楼下的高见
windily 2007-11-02
  • 打赏
  • 举报
回复
我以前编写小的程序通常是一个线程监听,再每个线程负责一个socket连接.
jennydin 2007-11-02
  • 打赏
  • 举报
回复
多线程监听就可以拉
qingtian6 2007-11-02
  • 打赏
  • 举报
回复
很好
很强大
wuziqi0310819 2007-11-02
  • 打赏
  • 举报
回复
顶。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧