服务端:import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class TestServer3 {
static int SERVER_TCP=8889;
public static void main(String[]args){

ServerSocket ss=null;
Socket s=null;
try {
ss=new ServerSocket(SERVER_TCP);
s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=dis.readUTF();
System.out.println("客户端信息:"+str);
System.out.println("客户端IP地址:"+s.getInetAddress());
System.out.println("客户端端口号:"+s.getPort());
System.out.println("本地端口号:"+s.getLocalPort());
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
dos.writeUTF(s.getInetAddress()+"你好,现在的服务器的时间为:"+(new Date())+".");
dis.close();
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestClient3 {
public static void main(String[]args){
Socket s=null;
int count=0;
try {
s=new Socket(args[0],TestServer3.SERVER_TCP);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
dos.writeUTF(args[1]);
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=dis.readUTF();
System.out.println(str);
dis.close();
dos.close();
s.close();
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
为什么我会出现java.lang.ArrayIndexOutOfBoundsException: 0
at TestClient3.main(TestClient3.java:13)
求指教

解决方案 »

  1.   

    你那个args[0],args[1]没有输入,所以出现这个错误
      

  2.   

    Socket socket = new Socket("127.0.0.1",4000);
    ServerSocket serverSocket = new ServerSocket(4000);
    换成这样的形式试一下
      

  3.   

    在客户端中:
    args[0]和args[1] 是指运行时传入的参数,建议修改为控制台输入参数。String str = new BufferedReader(new InputStreamReader(System.in)) ;//从控制台输入信息