protocol requirements are as follows :
1. Send "Connect" command, the server returns the "DATA RECEIVE PASSWORD"
2. Send password " 01234567 " , the server returns "CONNECT OK!"
I wrote a client to achieve a TCP / IP connection established by DataOutputStream input stream , while the character data into a byte in output in the console , but as a second step, not know how, after sending the password , the server can not return to respond , do not know what went wrong , please enlighten , source code is as follows :
import java.net.*;
import java.io.*;
public class ByteClient {
private int clientNumber;
private SocketAddress address;
public ByteClient(int clientNum) {
clientNumber = clientNum;
}
public void setupClients(String serverHostName, int port) throws IOException {
address = new InetSocketAddress(serverHostName, port);
System.out.println();
System.out.println("start client No. 1");
Socket socket = new Socket();
socket.connect(address);
//ADD
byte[] hello = "Connect".getBytes();
byte[] helloreturn="DATA RECEIVE PASSWORD...".getBytes();
byte[] code="01234567".getBytes();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.write(hello, 0, hello.length);
DataInputStream bufferedReader = new DataInputStream(socket.getInputStream());
byte[] cbuff = new byte[256];
char[] charBuff = new char[256];
int size = 0;
//ADD 判断是否接收到DATA RECIEVE PASSWORD
while( (size = bufferedReader.read(cbuff))> 0) {
convertByteToChar(cbuff, charBuff, size);
System.out.println(charBuff);//Console中显示了已收到的数据"DATA RECIEVE PASSWORD
}
//ADD
dos.write(code,0,code.length);//重复以上收发过程,怎么没有结果?
while( (size = bufferedReader.read(cbuff))> 0) {
convertByteToChar(cbuff, charBuff, size);
System.out.println(charBuff);
//ADD END
}
// bufferedReader.close();
//socket.close();
}
private void convertByteToChar(byte[] cbuff, char[] charBuff, int size) {
for(int i=0; i<charBuff.length; i++) {
if(i < size) {
charBuff[i] = (char)cbuff[i];
} else {
charBuff[i] = ' ';
}
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try{
ByteClient client = new ByteClient(4);
client.setupClients("XXX.XXX.XXX.XXX", 9999);//端口号和IP为服务器端IP,在此未列出
}
catch (IOException e) {
e.printStackTrace();
}
}
}
------ Solution ------------------------------------- -------
Socket 's InputSream is blocking , when you read it it would have been stuck there until the Socket closed or flow off.
So your program has been stuck in line 30
while ((size = bufferedReader.read (cbuff))> 0)
Incidentally that convertByteToChar method approach is not good , why not just use the built-in decoding method String , and you the only support ASCII character sets , such as if there is Chinese characters how to do ?
------ Solution ---------------------------------------- ----
Socket general practice is to use two threads , one to read, one to write and other control
Or you can use non-blocking SocketChannel
------ For reference only ------------------------- --------------
If you want to achieve the second step that I mentioned , what should be achieved? Create a socket?
Thank you, I try
------ For reference only ---------------------------- -----------
Results posted after the discovery, the problem is still not resolved, multi-threaded implementation is a server-side solution , but I can only do this program designed client , multi-threaded client how to deal with ?
没有评论:
发表评论