package com.netunit.workbench.test;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;public class ServerTest { private ServerSocket ss; private Socket socket;
PrintWriter out;
BufferedReader in; private int i = 0;
String line = "";
public ServerTest(){
try {
ss = new ServerSocket(7838);
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
// while(true){
// System.out.println(0);
// ss.setSoTimeout(50000);
byte[] b = new byte[1024];
b[0] = (byte) 0x55;
b[1] = (byte) 0xAA;
b[2] = (byte) 0x00;
b[3] = (byte) 0x01;
b[4] = (byte) 0x00;
b[5] = (byte) 0x00;
b[6] = (byte) 0x00;
b[7] = (byte) 0x00;
b[8] = (byte) 0x00;
b[9] = (byte) 0x00;
b[10] = (byte) 0x00;
b[11] = (byte) 0x00;
b[12] = (byte) 0x00;
b[13] = (byte) 0x00;
b[14] = (byte) 0x00;
b[15] = (byte) 0x00;
b[16] = (byte) 0x00;
OutputStream socketWriter = socket.getOutputStream();
socketWriter.write(b);
System.out.println("OK");
socketWriter.flush();
i =i+1; String line = "";
try {
// socket.setSoTimeout(60000);
line = in.readLine();
System.out.println("接收到来自SOCKET客户端的返回信息:" + line);
}catch(Exception e){
e.printStackTrace();
}
// }
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String []args){
new ServerTest();
}
}
客户端不停的发送包,我的服务器端监听自己7838端口接收数据,不知哪里有误,请大家帮忙指正,谢谢
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;public class ServerTest { private ServerSocket ss; private Socket socket;
PrintWriter out;
BufferedReader in; private int i = 0;
String line = "";
public ServerTest(){
try {
ss = new ServerSocket(7838);
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
// while(true){
// System.out.println(0);
// ss.setSoTimeout(50000);
byte[] b = new byte[1024];
b[0] = (byte) 0x55;
b[1] = (byte) 0xAA;
b[2] = (byte) 0x00;
b[3] = (byte) 0x01;
b[4] = (byte) 0x00;
b[5] = (byte) 0x00;
b[6] = (byte) 0x00;
b[7] = (byte) 0x00;
b[8] = (byte) 0x00;
b[9] = (byte) 0x00;
b[10] = (byte) 0x00;
b[11] = (byte) 0x00;
b[12] = (byte) 0x00;
b[13] = (byte) 0x00;
b[14] = (byte) 0x00;
b[15] = (byte) 0x00;
b[16] = (byte) 0x00;
OutputStream socketWriter = socket.getOutputStream();
socketWriter.write(b);
System.out.println("OK");
socketWriter.flush();
i =i+1; String line = "";
try {
// socket.setSoTimeout(60000);
line = in.readLine();
System.out.println("接收到来自SOCKET客户端的返回信息:" + line);
}catch(Exception e){
e.printStackTrace();
}
// }
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String []args){
new ServerTest();
}
}
客户端不停的发送包,我的服务器端监听自己7838端口接收数据,不知哪里有误,请大家帮忙指正,谢谢
public class ClientTest { public static void main(String args[])throws Exception{
Socket client=new Socket("127.0.0.1",7838);
PrintWriter pw=new PrintWriter(client.getOutputStream());
pw.println("1234565");
pw.flush();
client.close();
}
}打印结果
server:OK
接收到来自SOCKET客户端的返回信息:1234565