Java Socket??????????????????
???????????? ???????[ 2012/12/4 10:06:23 ] ????????
???????????????£?
package com.gloomyfish.socket.tutorial.two;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class HelloClient {
private int clientNumber;
private SocketAddress address;
public HelloClient(int clientNum) {
clientNumber = clientNum;
}
public void setupClients(String serverHostName?? int port) throws IOException {
address = new InetSocketAddress(serverHostName?? port);
for(int i=0; i<clientNumber; i++) {
System.out.println();
System.out.println("start client No. " + (i+1));
Socket socket = new Socket();
socket.connect(address);
DataInputStream bufferedReader = new DataInputStream(socket.getInputStream());
byte[] cbuff = new byte[256];
char[] charBuff = new char[256];
int size = 0;
while( (size = bufferedReader.read(cbuff))> 0) {
convertByteToChar(cbuff?? charBuff?? size);
System.out.println(charBuff);
}
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) {
try {
HelloClient client = new HelloClient(10);
client.setupClients("localhost"?? 9999);
} catch (IOException e) {
e.printStackTrace();
}
}
}
???????10??????????????????????????????????????????????????
???????????????????????????charBuff
???????????н????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11