客户端服务器udp多线程套接字编程中的问题 [英] problem in client server udp multithread socket programming

查看:84
本文介绍了客户端服务器udp多线程套接字编程中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我已经开发了客户端服务器单向通信。之后工作正常



i开发了双向通信。我将解释单向和双向通信。 -way communcation



假设客户发送一个请求(消息),如HI,那么服务器会听取请求



并且它在服务器端显示Himeasge然后在服务器没有响应客户端时



request.it仅在服务器端显示用户请求。这是单向的



通信,但每当服务器响应客户端请求时如Hello那么称为



双向沟通。我的项目没有进行双向沟通。真的告诉你



minium它没有单向通信(客户请求不会发送到服务器)



在我的项目中也是。




clientside udp套接字程序代码:



< pre lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Net;
使用 System.Net.Sockets;

命名空间 clientserver1
{
class chart1
{
private static System.Threading.Thread clientThread;
静态 void Main( string [] args)
{
CreateThreads();
}
私有 静态 void CreateThreads()
{
clientThread = new System.Threading.Thread( new
System.Threading.ThreadStart(RunClientThread));
clientThread.Start();
}


私有 静态 void RunClientThread()
{
string sendStr = ;
UdpClient theClient = new UdpClient( 192.168 .0.5 9050 );
while (!sendStr.Trim()。ToUpper()。Equals( END))
{
sendStr = Console.ReadLine();
byte [] myData = new byte [ 1024 ];
myData = Encoding.ASCII.GetBytes(sendStr);
theClient.Send(myData,myData.Length);
}
theClient.Close();
}

private static void RunServerThread()
{
string rcvData = ;
IPEndPoint IPEP = new IPEndPoint(IPAddress.Any, 9050 );
UdpClient theSock = new UdpClient(IPEP);来自客户的
IPEndPoint;
while (!rcvData.Trim()。ToUpper()。Equals( END))
{
byte [] myData = new byte [ 1024 ];
fromClient = new IPEndPoint(IPAddress.Any, 0 );
myData = theSock.Receive( ref fromClient);
rcvData = Encoding.ASCII.GetString(myData);
Console.WriteLine(fromClient.ToString()+ + rcvData);
}
theSock.Close();
}

}
}





serverside udp套接字程序代码:



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

命名空间 ClientServer_2
{
class chart2
{
private static System.Threading.Thread clientThread;

静态 void Main( string [] args)
{
CreateThreads();
}
私有 静态 void CreateThreads()
{
clientThread = new System.Threading.Thread( new
System.Threading.ThreadStart(RunServerThread));
clientThread.Start();
}

private static void RunClientThread()
{
string sendStr = ;
UdpClient theClient = new UdpClient( 192.168 .0.5 9050 );
while (!sendStr.Trim()。ToUpper()。Equals( END))
{
sendStr = Console.ReadLine();
byte [] myData = new byte [ 1024 ];
myData = Encoding.ASCII.GetBytes(sendStr);
theClient.Send(myData,myData.Length);
}
theClient.Close();
}

private static void RunServerThread()
{
string rcvData = ;
IPEndPoint IPEP = new IPEndPoint(IPAddress.Any, 9050 );
UdpClient theSock = new UdpClient(IPEP);来自客户的
IPEndPoint;
while (!rcvData.Trim()。ToUpper()。Equals( END))
{
byte [] myData = new byte [ 1024 ];
fromClient = new IPEndPoint(IPAddress.Any, 0 );
myData = theSock.Receive( ref fromClient);
rcvData = Encoding.ASCII.GetString(myData);
Console.WriteLine(fromClient.ToString()+ + rcvData);
}
theSock.Close();
}
}
}





以上两个应用程序运行相同的机器



客户端udp套接字程序和serverside udp套接字程序是相同的,因为两个应用程序在同一台机器上运行。如果应用程序运行在不同的



机器然后源代码将是相同的,除了在


$ b中指定的IP地址$ b RunClientThread()在serverside udp socket program.i认为我的代码是正确的。如果任何



错误的逻辑将在那里修改代码。







如果你想要任何参考(不是.dll)项目.pls告诉我你的mailid。我将



转发客户端 - 服务器pdf.i基于客户端 - 服务器开发项目pdf



你可以解决我的双向沟通​​吗?





谢谢你。

解决方案

我建​​议您查看MSDN上免费提供的这些信息。



套接字代码示例 [ ^ ]



在那里你可以找到同步和异步客户端和服务器的代码示例。

在您的情况下,我猜这个异步示例最适合。

您将使用这些方法而不是使用自己的线程例如,BeginReceive和EndReceive。



我过去自己在这段代码上构建了应用程序。您总是需要根据需要更改代码,但这是一个很好的例子。


Actually i had developed client server one-way commucation.it's working fine after that

i had developed two-way communcation.i will explain one-way and two-way communcation

For suppose client send one Request(message) like "HI" then server listening the request

and it's shows "Hi" measge on server side then at the time server not responding client

request.it's only take user request show on server side only.this is one-way

communcation but whenever server responding for client request like "Hello" then called

two-way communcation.my project not working two-way communication.really tell u

minium it's not working one-way communcation(client request will not send to server)

in my project also.


clientside udp socket program code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace clientserver1
{
    class chart1
    {
        private static System.Threading.Thread clientThread;
        static void Main(string[] args)
        {
            CreateThreads();
        }
        private static void CreateThreads()
        {
            clientThread = new System.Threading.Thread(new
            System.Threading.ThreadStart(RunClientThread));
            clientThread.Start();
        }
        

        private static void RunClientThread()
        {
            string sendStr = "";
            UdpClient theClient = new UdpClient("192.168.0.5",9050);
            while (!sendStr.Trim().ToUpper().Equals("END"))
            {
                sendStr = Console.ReadLine();
                byte[] myData = new byte[1024];
                myData = Encoding.ASCII.GetBytes(sendStr);
                theClient.Send(myData, myData.Length);
            }
            theClient.Close();
        }

        private static void RunServerThread()
        {
            string rcvData = "";
            IPEndPoint IPEP = new IPEndPoint(IPAddress.Any, 9050);
            UdpClient theSock = new UdpClient(IPEP);
            IPEndPoint fromClient;
            while (!rcvData.Trim().ToUpper().Equals("END"))
            {
                byte[] myData = new byte[1024];
                fromClient = new IPEndPoint(IPAddress.Any, 0);
                myData = theSock.Receive(ref fromClient);
                rcvData = Encoding.ASCII.GetString(myData);
                Console.WriteLine(fromClient.ToString() + " " + rcvData);
            }
            theSock.Close();
        }

    }
}



serverside udp socket program code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace ClientServer_2
{
    class chart2
    {
        private static System.Threading.Thread clientThread;

        static void Main(string[] args)
        {
            CreateThreads();
        }
        private static void CreateThreads()
        {
            clientThread = new System.Threading.Thread(new
            System.Threading.ThreadStart(RunServerThread));
            clientThread.Start();
        }

        private static void RunClientThread()
        {
            string sendStr = "";
            UdpClient theClient = new UdpClient("192.168.0.5",9050);
            while (!sendStr.Trim().ToUpper().Equals("END"))
            {
                sendStr = Console.ReadLine();
                byte[] myData = new byte[1024];
                myData = Encoding.ASCII.GetBytes(sendStr);
                theClient.Send(myData, myData.Length);
            }
            theClient.Close();
        }

        private static void RunServerThread()
        {
            string rcvData = "";
            IPEndPoint IPEP = new IPEndPoint(IPAddress.Any, 9050);
            UdpClient theSock = new UdpClient(IPEP);
            IPEndPoint fromClient;
            while (!rcvData.Trim().ToUpper().Equals("END"))
            {
                byte[] myData = new byte[1024];
                fromClient = new IPEndPoint(IPAddress.Any, 0);
                myData = theSock.Receive(ref fromClient);
                rcvData = Encoding.ASCII.GetString(myData);
                Console.WriteLine(fromClient.ToString() + " " + rcvData);
            }
            theSock.Close();
        }
    }
}



above both applications are running same machine

clientside udp socket program and serverside udp socket program are same due to both

applications are runing on same machine.if the applications are running on different

machines then The source code will be identical except for IP address specified in the

RunClientThread( ) in serverside udp socket program.i think my code is correct.if any

wrong logic will be there pls modify the code.



if u want to any reference(not .dll) to the project .pls tell me ur mailid. i will

forward the client-server pdf.i had developed the project based on client-server pdf

can u please solve me for two-way communcation?


thank u.

解决方案

I recommend that you take a look at this information, freely available on MSDN.

Socket Code Examples[^]

There you can find code examples for both synchronous and asynchronous client and server.
In your case I guess the asynchronous example is best suited.
Instead of using your own thread you will use the methods BeginReceive and EndReceive, for example.

I have built applications on this code myself in the past. You always have to change the code to your needs, but it is a good example to start with.


这篇关于客户端服务器udp多线程套接字编程中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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