java socket测试
package com.vfsd.core; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class VFSDClientServer { public static void main(String[] args) { //String hex = Integer.toHexString(var); String addressIP="115.26.69.66"; int addressPort=8779; startServerSocket(addressIP,addressPort); } public static void startServerSocket(String addressIP,int addressPort) { try { //serverSocket = new ServerSocket(addressPort); Socket acceptSocket = new Socket(addressIP,addressPort); DataInputStream dis = new DataInputStream(acceptSocket.getInputStream()); DataOutputStream dos = new DataOutputStream(acceptSocket.getOutputStream()); while(true) { //boolean isClosed = isServerClose(acceptSocket);//判断是否断开 boolean isClosed = acceptSocket.isClosed(); if(true) { //InetAddress spcketAddress = acceptSocket.getInetAddress(); //String hostName = spcketAddress.getHostAddress(); //String receiveMsg = dis.readUTF(); //System.out.println("host name:"+hostName+" msg:"+receiveMsg); sendData(dos); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void sendData(DataOutputStream dos) { String dataStr = createRandomData3(); try { System.out.println("========================================="); System.out.println(dataStr); dos.writeUTF(dataStr); //dos.write(dataStr.getBytes()); //dos.writeBytes(dataStr); //byte[] bytes1 = new byte[dataStr.length()]; /*for(int i=0;i<dataStr.length();i++) { //bytes1[i] = dataStr.ge }*/ //dos.writeBytes(dataStr); dos.flush(); //byte[] bytes = new byte[2400]; //dos.write(dataStr.getBytes()); //dos.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String createRandomData3() { //int x_len = (int) (Math.random()*60+10); //int y_len = (int) (Math.random()*60+10); int x_len = 60; int y_len = 20; java.text.DecimalFormat df =new java.text.DecimalFormat("#.00"); //int z_len = x*y; String dataStr="abcd"; for(int x=0;x<x_len;x++) { for(int y=0;y<y_len;y++) { double zd = Math.random()*10; String zStr = df.format(zd); double zd2 = Double.parseDouble(zStr); int z = (int) (zd2*100); //dataStr=dataStr+x+" "+y+" "+z+" "; dataStr=dataStr+Integer.toHexString(z)+""; } } //System.out.println(dataStr); return dataStr; } }