`
孤独舞者
  • 浏览: 85888 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java C# socket通讯自己

    博客分类:
  • java
阅读更多
接到一个项目需要java(swt/swing)与。net(其中的winForm)进行交互。进行互动连动,在王上找到一些文章但是没有一个合适的。可以通过读取文件,可以使用socket还有其它方式但是没有想到。这里选择了socket通信。

首先一
定义变量:
#region//定义变量
        IPAddress HostIP = IPAddress.Parse("127.0.0.1");
        IPEndPoint point;
        Socket socket;
        bool flag = true;
        Socket acceptedSocket;
        #endregion




        #region



通过winForm建立一个socket(模式c#中部分serversocket 和client socket)。
        public void startServer()
        {
            HostIP = IPAddress.Parse("127.0.0.1");
            try
            {
                point = new IPEndPoint(HostIP, Int32.Parse("8866"));
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(point);
                socket.Listen(50);
                acceptedSocket = socket.Accept();
                Thread thread = new Thread(new ThreadStart(Proccess));
                thread.Start();
            }
            catch (Exception ey)
            {
                MessageBox.Show(ey.Message);
            }
        }
其次就是 定义里面的 Process函数


      private void Proccess()
        {
            if (acceptedSocket.Connected)
            {
                while (flag)
                {
                    byte[] receiveByte = new byte[1024];
                    acceptedSocket.Receive(receiveByte, receiveByte.Length, 0);
                    string strInfo = Encoding.UTF8.GetString(receiveByte);
                
                }
           
            }
        }
这样就启动了服务端的进程(其时process就是一个接收socket发来的信心的线程)。
然后是发送信息函数:
private void SendToChart(string  str){

           
               Byte[] sendByte = new Byte[1024];
               sendByte = Encoding.UTF8.GetBytes(str.ToCharArray());
               acceptedSocket.Send(sendByte, sendByte.Length, 0);
           
          
        }
用UTF-8格式读取string字符串为byte数组。然后发送。
现在是java端(也就是这里的client端)



public class SocketToC {


private Socket so;
private DataOutputStream dos;
private DataInputStream dis;
private String message;
private byte[]  b=new byte[1024];
public SocketToC(){


try{
so=new Socket("127.0.0.1",11000);

}catch(Exception e){
e.printStackTrace();
}






}
public Socket getSo() {
return so;
}
public void setSo(Socket so) {
this.so = so;
}

public boolean sendMsg(String flag){
   
try {
this.message=flag ;
dos=new DataOutputStream(so.getOutputStream());
dos.write(message.getBytes("UTF-8"));
} catch (IOException e) {

}


return true;
}
public String getMsg()
{
try {

dis =new  DataInputStream(so.getInputStream());
dis.read(b,0, b.length);
String stri=new String(b,"UTF-8");

} catch (IOException e) {

e.printStackTrace();
}
return message;

}




}
ok就是这么多了等这里需要注意的是 发送和接收的时候的格式要统一
发送的时候需要:dos.write(message.getBytes("UTF-8"));发送UTF-8格式的二进制数据流。
接收字符串:然后再new一次 注意相应的格式这里使用UTF-8格式:
dis.read(b,0, b.length);
        String stri=new String(b,"UTF-8");
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics