用eclipse软件java聊天程序代码的客户端和服务器怎么设置,如何在Java Eclipse中使用Socket编程运行服务器客户端代码?...

本文介绍了一个Java程序,涉及使用Socket进行客户端与服务器之间的简单通信。在尝试运行客户端时遇到ArrayIndexOutOfBoundsException错误。文章将解释问题原因并提供可能的解决方案,重点在于正确连接命令行参数和Socket实例化。
摘要由CSDN通过智能技术生成

Client code:

import java.io.*;

import java.net.*;

public class SimpleClient {

public static void main(String[] args) throws IOException {

Socket clientSocket = null;

try {

clientSocket = new Socket(args[0], 4442);

} catch (UnknownHostException e) {

System.err.println("Don't know about host: " + args[0] + ".");

System.exit(1);

} catch (IOException e) {

System.err.println("Couldn't get I/O for " +

"the connection to: " + args[0] + "");

System.exit(1);

}

BufferedInputStream in;

BufferedOutputStream out;

try {

in = new BufferedInputStream(clientSocket.getInputStream());

out = new BufferedOutputStream(clientSocket.getOutputStream());

} catch (IOException e) {

System.out.println(e.toString());

return;

}

byte[] m_txt = args[1].getBytes();

out.write(m_txt, 0, m_txt.length);

out.flush();

byte[] m_rcv = new byte[m_txt.length];

int n = in.read(m_rcv, 0, m_rcv.length);

if (n != m_rcv.length) {

System.out.println("Some data are lost ...");

}

System.out.println(new String(m_rcv));

out.close();

in.close();

clientSocket.close();

}

}

Server:

import java.io.*;

import java.net.*;

public class SimpleServer {

public static void main(String[] args) throws IOException {

boolean listening = true;

ServerSocket serverSocket = null;

try {

serverSocket = new ServerSocket(4442);

} catch (IOException e) {

System.err.println("Could not listen on port: 4444.");

System.exit(1);

}

while(listening) {

Socket clientSocket = serverSocket.accept();

(new SimpleConHandler(clientSocket)).start();

}

serverSocket.close();

}

}

Connection Handler:

import java.io.*;

import java.net.*;

public class SimpleConHandler extends Thread

{

private Socket clientSocket;

public SimpleConHandler(Socket clientSocket) {

this.clientSocket = clientSocket;

}

public void run() {

BufferedInputStream in;

BufferedOutputStream out;

try {

in = new BufferedInputStream(clientSocket.getInputStream());

out = new BufferedOutputStream(clientSocket.getOutputStream());

} catch (IOException e) {

System.out.println(e.toString());

return;

}

try {

byte[] msg = new byte[4096];

int bytesRead = 0;

int n;

while((n = in.read(msg, bytesRead, 256)) != -1) {

bytesRead += n;

if (bytesRead == 4096) {

break;

}

if (in.available() == 0) {

break;

}

}

for(int i=bytesRead; i>0; i--) {

out.write(msg[i-1]);

}

out.flush();

} catch(IOException e1) {

System.out.println(e1.toString());

}

try {

out.close();

in.close();

clientSocket.close();

} catch ( IOException e2 ) {;}

}

}

First i RUN Server, but when i try to RUN Client, the error which i am getting is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

at SimpleClient.main(SimpleClient.java:11)

May be i have to use different consoles to run both the Server and the Client? If so, then please tell me the way. i am using Java Eclipse 1.6 SE.

解决方案

clientSocket = new Socket(args[0], 4442);

Your program needs a command line argument:

java your.Program

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值