Java Socket 入门2 Java与C# Socket 通信

参考http://www.cnblogs.com/cdtarena/p/3184313.html

这里以C#作为服务端  其实不论C#是服务端还是客户端都不是主要问题

毕竟不论客户端还是服务端 都包括了发送和接收两个部分

C#

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

/*转载http://www.cnblogs.com/jason_yjau/archive/2009/06/24/1510580.html*/
namespace SocketOne
{
    class MyServer
    {
        static void Main(string[] args)
        {
            try
            {
                int port = 8081;
                string host = "127.0.0.1";
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);
                Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket类
                s.Bind(ipe);//绑定8081端口
                s.Listen(0);//开始监听
                Console.WriteLine("Wait for connect");
                Socket temp = s.Accept();//为新建连接创建新的Socket。
                Console.WriteLine("Get a connect");
                string recvStr = "";
                string sendStr = "";

                //int count = 0;
                while (true)
                {

                    //接收传送信息的时候都是发送的byte数组
                    //首先向客户端发送信息                    
                    sendStr = Console.ReadLine();
                    byte[] recvBytes = new byte[1024];
                    //与java通信 后面加个\n  因为我的java用的是readline
                    byte[] bs = Encoding.ASCII.GetBytes(sendStr+"\n");
                    temp.Send(bs, bs.Length, 0);//返回客户端成功信息


                    //接收信息
                    int bytes;
                    bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接受信息
                    recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);//
                    Console.WriteLine("Server Get Message:{0}", recvStr);//把客户端传来的信息显示出来


                }
                //temp.Close();
                //s.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            Console.WriteLine("Press Enter to Exit");
            Console.ReadLine();
        }
    }
}

 

Java

package socketToCSharp;

import java.io.*;
import java.net.*;

public class Client {
    Socket socket;
    BufferedReader in;
    PrintWriter out;

    public Client() {
        try {
            socket = new Socket("127.0.0.1", 8081);
            
            in = new BufferedReader(new InputStreamReader(socket
                    .getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader line = new BufferedReader(new InputStreamReader(
                    System.in));
            
            String str=null;
            String msg=null;
            while(true){
                //System.out.println("client loop");
                str=in.readLine();
                System.out.println("servdr says "+str);
                
                //java在发送和接受信息时  都要语句后以\n结尾的
                //C#恰好相反  因此 C#给java发信息  语句末尾要加\n 
                //java给C#发信息  末尾去掉\n
                msg="client";
                msg=line.readLine();
                msg=msg.replace('\n', ' ');
                out.println(msg);
                //out.flush();
                
            }
//            line.close();
//            out.close();
//            in.close();
//            socket.close();
        } catch (IOException e) {
        }
    }
    public static void main(String[] args) {
        new Client();
    }
}

 

 

posted @ 2013-10-18 19:20  cart55free99  阅读(360)  评论(0编辑  收藏  举报