异步套接字编程 [英] Asynchronous Socket programming

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

问题描述


我是电气工程专业的,对编程我了解甚少.
您能帮助我开发用于单服务器,多客户端(几百个)通信(TCP/IP协议)的应用程序吗?这种通信必须是异步的,并且服务器能够答复客户端请求的数据.
并且必须将答复发送到已发送请求的特定客户端.


我浏览了此博客中的几篇文章,但无法解决我的问题



希望您能尽快答复我,并帮我解决这个难题,并在我的项目中对我有所帮助.
谢谢
Tarun kashyap


I from electrical engineering and i know very little about programming.
can you help me with developing an application for single server multiple client(few hundreds) communication(TCP/IP protocol), which has to be asynchronous and server capable of replying to the data requested by the client.
And the reply must be sent to a particular client that has sent the request.


I have gone through few articles in this blog but it couldn''t solve my problem



hope you will reply me soon and do me this needful and help me in my project.

Thank You
Tarun kashyap

推荐答案

您到目前为止尝试了什么?尝试运用从阅读您提到的文章中学到的知识.代码项目中有很多关于您提到的主题的文章.尝试一下并提出一些具体问题,当人们看到您的努力时,他们会非常乐意提供帮助.
What have you tried till now? Try to apply what ever knowledge you have gained from reading the articles you mentioned. Code project has a lot of articles on the topics you have mentioned. Give it a try and ask specific questions, people would be more than willing to help when they see your effort.


非常感谢您对我的帖子的回复.


我已经根据一些文章编写了代码库,并且能够从多个客户端连接到服务器.我的客户端应用程序是Windows窗体应用程序,服务器是控制台应用程序.连接到几个客户端并尝试将数据从客户端发送到服务器后,发生了重叠错误(实际上我的办公室里有台式机,所以我无法解释确切的错误).而且,一旦我尝试关闭连接的所有客户端,服务器控制台就会在某种程度上损坏.错误消息在屏幕上滚动,这是不可读的.
Thank you very much for responding to my post.


I have written a code base on some articles and i am able to connect to the server from multiple clients. My client application is a windows form application and server is a console application. After connecting to few clients and try to send the data from the client to server there is a overlap error that is occurring(actually i have my desktop in my office so i am unable to explain about the exact error). And once i try to close any of the clients connected the server console is getting corrupted in a sense an error message is scrolling on the screen which is unreadable.


服务器程序(控制台应用程序)
使用系统;
使用System.Threading;
使用System.Net.Sockets;
使用System.Text;
使用System.Net;
使用System.IO;

命名空间ConsoleApplication
{
课程计划
{
public Socket socketStream;
私人BinaryWriter作家;
私人BinaryReader掠夺者;



静态void Main(string [] args)
{


//IPEndPoint serverSocket =新的IPEndPoint(IPAddress.Any,1215);

Int32端口= 1215;
IPAddress localAddr = IPAddress.Parse("10.14.40.228");
TcpListener serverSocket =新的TcpListener(localAddr,port);
TcpClient clientSocket =默认值(TcpClient);
int计数器= 0;

serverSocket.Start();
Console.WriteLine(>>" +服务器已启动");

计数器= 0;
而(true)
{
计数器+ = 1;
clientSocket = serverSocket.AcceptTcpClient();
socketStream =新的NetworkStream(clientSocket);
Console.WriteLine(>>" +客户端编号:" + Convert.ToString(计数器)+开始!");
handleClinet客户端=新的handleClinet();
client.startClient(clientSocket,Convert.ToString(counter));
}

clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(>>" +退出");
Console.ReadLine();
}
}

//分别处理每个客户请求的类
公共类handleClinet
{
TcpClient clientSocket;
字符串clNo;
public void startClient(TcpClient inClientSocket,字符串clineNo)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
线程ctThread = new Thread(doChat);
ctThread.Start();
}
私有void doChat()
{
int requestCount = 0;
byte [] bytesFrom =新的byte [10025];
字符串dataFromClient = null;
Byte [] sendBytes = null;
字符串serverResponse = null;
字符串rCount = null;
requestCount = 0;

而((true))
{
试试
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom,0,(int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0,dataFromClient.IndexOf("
Server program (Console application)
using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication
{
class Program
{
public Socket socketStream;
private BinaryWriter writer;
private BinaryReader rader;



static void Main(string[] args)
{


//IPEndPoint serverSocket = new IPEndPoint(IPAddress.Any, 1215);

Int32 port = 1215;
IPAddress localAddr = IPAddress.Parse("10.14.40.228");
TcpListener serverSocket = new TcpListener(localAddr, port);
TcpClient clientSocket = default(TcpClient);
int counter = 0;

serverSocket.Start();
Console.WriteLine(" >> " + "Server Started");

counter = 0;
while (true)
{
counter += 1;
clientSocket = serverSocket.AcceptTcpClient();
socketStream = new NetworkStream(clientSocket);
Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
handleClinet client = new handleClinet();
client.startClient(clientSocket, Convert.ToString(counter));
}

clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> " + "exit");
Console.ReadLine();
}
}

//Class to handle each client request separatly
public class handleClinet
{
TcpClient clientSocket;
string clNo;
public void startClient(TcpClient inClientSocket, string clineNo)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
Byte[] sendBytes = null;
string serverResponse = null;
string rCount = null;
requestCount = 0;

while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("


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

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