java socket与 c socket通讯 java服务端

来源:互联网 发布:栅格数据和矢量数据 编辑:程序博客网 时间:2024/06/05 00:15
Jclient.java:

public class Jclient {

    public static void main(String[] args) {
        
        Socket JavaSocket = null;  
        DataOutputStream os = null;
        DataInputStream  is  =null;
        
        try {
            JavaSocket= new Socket("210.72.13.72",9734);
            os = new DataOutputStream(JavaSocket.getOutputStream());
            is = new DataInputStream(JavaSocket.getInputStream());
        
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to");
        }


if (JavaSocket != null && os != null && is != null)
{
            try {

String jcStr="this is Java 客户端 send string";
byte[] jcBytes =jcStr.getBytes();
                os.write(jcBytes);//使用byte[]发送数据包
                os.flush();

byte[] cbuf=new byte[8096];
is.read(cbuf);
String responseLine=new String(cbuf);        
System.out.println("读取到的String是:"+responseLine);

os.close();
                is.close();
                JavaSocket.close();   
            } catch (UnknownHostException e) {
                System.err.println("Trying to connect to unknown host: " + e);
            } catch (IOException e) {
                System.err.println("IOException:  " + e);
            }
        }
    }           
}
原创粉丝点击