2013年9月26日星期四

Java and C # UDP communication problem

Java as a UDP server
C # as the UDP client.
Source Code


package com.taoge.socke;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPEchoServer {
private static final int ECHOMAX=255;//max size of echo datagram

public static void main(String[] args) throws IOException {
/*
if(args.length!=1){
throw new IllegalArgumentException("Parameter(s):<Port>");
}
*/
//int servPort=Integer.parseInt(args[0]);
int servPort = 5555;

//1.创建一个DatagramSocket实例,指定本地端口号,可以选择指定本地地址
DatagramSocket socket=new DatagramSocket(servPort);
DatagramPacket packet=new DatagramPacket(new byte[ECHOMAX],ECHOMAX);

while(true){
//2.使用DatagramSocket的receive方法来接收一个DatagramPacket实例。
socket.receive(packet);
System.out.println("Handling client at "+packet.getAddress().getHostAddress()+" on port "+packet.getPort());
System.out.println("包的数据:" + new String(packet.getData()));
socket.send(packet);
packet.setLength(ECHOMAX);
//socket.close();
}
}
}


C # part


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

namespace ConsoleApplication1
{

    /**
     * C# 版本UDP客户端,用与连接Java服务器端发送数据。
     * */
    class Program
    {
       

        static void Main(string[] args)
        {
            int SenderPort = 5555;
            String host = "127.0.0.1";

            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipepoit = new IPEndPoint(ip,SenderPort);

            UdpClient udpClient = new UdpClient(SenderPort);
            udpClient.Connect(ip,SenderPort);

            Byte[] sendByts = Encoding.ASCII.GetBytes("welcome to chengdu!!");

            //udpClient.Send(sendByts,sendByts.Length);
            udpClient.Send(sendByts, 255);
            //IPEndPoint RemoteIpEndPoint = new IPEndPoint(ip, 0);

            //Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
            //string returnData = Encoding.ASCII.GetString(receiveBytes);

            //Console.WriteLine("This is the message you received " + returnData.ToString());
            //Console.WriteLine("This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString());
            Console.WriteLine("发送数据完毕!!");
            udpClient.Close();

        }
    }
}



now send messages to the Java C # , Java can not receive this message .

Java to Java send no problem , C # to C # to send no problem , now is the C # to Java problems.
------ Solution ---------------------------------------- ----
expressed c # learned , there is pressure
------ Solution - -------------------------------------------
do with mina < br> ------ Solution ----------------------------------------- ---
I like to use the socket, the original flavor of Kazakhstan , Java did not make changes , C # is slightly changed a bit , Java clients can receive data .


            int SenderPort = 5555;
            String host = "127.0.0.1";
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Parse(host),SenderPort);

            Byte[] sendByts = Encoding.ASCII.GetBytes("welcome to chengdu!!");
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.SendTo(sendByts,sendByts.Length, SocketFlags.None, RemoteIpEndPoint);
            

            Console.WriteLine("发送数据完毕!!");
            socket.Close();

------ Solution ------------------------------------- -------
should message is received , but when sent from C # Java does not know where the demarcation point is the message frame and still cached in the message queue.
------ For reference only -------------------------------------- -


In fact, I have learned , but also to write the C # program , which means that pressure Alexander !
------ For reference only -------------------------------------- -
anyone know this problem?
------ For reference only -------------------------------------- -


What's this ?
------ For reference only -------------------------------------- -

yesterday I tested , there is no problem with the way the socket , using C # udpclient class is not, or use socket forget.
------ For reference only -------------------------------------- -
udpclient inside of the socket of the package, I am also not familiar with it .

udpClient.Send (sendByts, 255); This line should be an error , because the packets are not so long , the other seems to be no problem .
------ For reference only -------------------------------------- -
Hello ! I also encountered the same problem with you . UDP mode of communication is how you ask the next solution .

没有评论:

发表评论