更新时间:2022-10-25 09:50:39
我建议您查看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.