一个超简单的telnet客户端,为什么连不上? 我试了你的代码,没问题啊!我用jdk1.4.2编译的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 编译没错误不过你的服务器应用程序应该用1024以上的端口因为1024以下的端口一般为知名的网络服务或系统服务所用另外,DataInputStream的readLine()方法sun不推荐使用 谢谢大家!对了,好像我没有说清楚,我是想做一个telnet的客户端,执行起来就好像在dos窗口下的命令行一样,但是我没有得到这样的效果,请问问题出在什么地方?如何解决?谢谢了! 刚做的一个,实现聊天服务器端程序:import java.io.*;import java.net.*;class TcpSever{ private static ServerSocket server=null; public static void main(String[] agrs) { try { server=new ServerSocket(3000); while(true) { Socket s=server.accept(); (new Thread(new DialogToClient(s))).start(); } } catch(Exception e) { e.printStackTrace(); } }}class DialogToClient implements Runnable{ private Socket DialogSocket=null; public DialogToClient(Socket DialogSocket) { this.DialogSocket=DialogSocket; } public void run() { try { //System.out.println("in DialogToClient"); InputStream input=DialogSocket.getInputStream(); OutputStream output=DialogSocket.getOutputStream(); (new Thread(new InputData(input))).start(); (new Thread(new OutputData(output))).start(); } catch(Exception e) { } }}class OutputData implements Runnable{ private BufferedReader read=null; private OutputStream output=null; public OutputData(OutputStream output) { read=new BufferedReader(new InputStreamReader(System.in)); this.output=output; } public void run() { while(true) { if(Flag.f.getFlag()==false) break; try { output.write(((read.readLine())+System.getProperty("line.separator")).getBytes()); } catch(Exception e) { } } try { read.close(); output.close(); } catch(Exception e) { } }}class InputData implements Runnable{ private BufferedReader input=null; public InputData(InputStream input) { this.input=new BufferedReader(new InputStreamReader(input)); } public void run() { while(true) { byte[] b=new byte[2000]; try { String str=input.readLine(); if(str.equals("end")) break; System.out.println("client: "+str); } catch(Exception e) { } } Flag.f.setFlag(false); try { input.close(); } catch(Exception e) { } } }class Flag{ private boolean flag=true; public void setFlag(boolean flag) { this.flag=flag; } public boolean getFlag() { return flag; } private Flag() { } public static Flag f=new Flag();}客户端程序:import java.net.*;import java.io.*;class TcpClient{ private BufferedReader read=null; private OutputStream output=null; public TcpClient(Socket client) { try { read=new BufferedReader(new InputStreamReader(System.in)); output=client.getOutputStream(); } catch(Exception e) { System.out.println("error in TcpClient constractor"); } } public void writeData() { while(true) { try { String str=read.readLine(); output.write((str+System.getProperty("line.separator")).getBytes()); if(str.equals("end")) break; } catch(Exception e) { System.out.println("error in writeData"); } } release(); } public void release() { try { read.close(); output.close(); } catch(Exception e) { System.out.println("error in release"); } } public static void main(String[] agrs) { try { Socket client=new Socket(InetAddress.getByName("192.168.1.8"),3000); Thread t=new Thread(new DialogToServer_ReadData(client)); t.setDaemon(true); t.start(); TcpClient x=new TcpClient(client); x.writeData(); client.close(); } catch(Exception e) { System.out.println("error in main"); } }}class DialogToServer_ReadData implements Runnable { private BufferedReader input=null; public DialogToServer_ReadData(Socket client) { try { input=new BufferedReader(new InputStreamReader(client.getInputStream())); } catch(Exception e) { System.out.println("error in DialogToServer_ReadData constractor"); } } public void run() { while(true) { try { System.out.println("server: "+input.readLine()); } catch(Exception e) { System.out.println("error in run"); } } }}可在同一机器上运行,要先运行服务器程序关闭时,应先关闭客户机程序 谢谢大家,可是我的目的是想实现与标准的telnet服务器进行通信,可能要遵守telnet协议吧,但是我不知道如何来做,谁有简单的telnet客户端程序,请贴出来好吗?谢谢! 引用那点问题 socket:客户端接收不了服务器的消息 初学 applet的问题 多边形 如何开始Java的学习 第一次用xp,怎样设置环境变量? 接口是否具有类一样的层次结构,可以追溯到源头,像类的object ? 小弟希望各位高﹐中﹐低手能提供我一些關于JAVA打印的技朮資料﹐謝謝 java management API(jmAPI) 是什么? 关于Java版的贪吃蛇问题,蛇身初始化为3个以上时,蛇就直接死了 Java中有没有和.net framework中DataSet类似的类。高分求取。 初学者的问题,大家帮忙阿
不过你的服务器应用程序应该用1024以上的端口
因为1024以下的端口一般为知名的网络服务或系统服务所用另外,DataInputStream的readLine()方法sun不推荐使用
服务器端程序:
import java.io.*;
import java.net.*;class TcpSever
{
private static ServerSocket server=null;
public static void main(String[] agrs)
{
try
{
server=new ServerSocket(3000);
while(true)
{
Socket s=server.accept();
(new Thread(new DialogToClient(s))).start();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}class DialogToClient implements Runnable
{
private Socket DialogSocket=null;
public DialogToClient(Socket DialogSocket)
{
this.DialogSocket=DialogSocket;
}
public void run()
{
try
{
//System.out.println("in DialogToClient");
InputStream input=DialogSocket.getInputStream();
OutputStream output=DialogSocket.getOutputStream();
(new Thread(new InputData(input))).start();
(new Thread(new OutputData(output))).start();
}
catch(Exception e)
{
}
}
}class OutputData implements Runnable
{
private BufferedReader read=null;
private OutputStream output=null;
public OutputData(OutputStream output)
{
read=new BufferedReader(new InputStreamReader(System.in));
this.output=output;
}
public void run()
{
while(true)
{
if(Flag.f.getFlag()==false)
break;
try
{
output.write(((read.readLine())+System.getProperty("line.separator")).getBytes());
}
catch(Exception e)
{
}
}
try
{
read.close();
output.close();
}
catch(Exception e)
{
}
}
}class InputData implements Runnable
{
private BufferedReader input=null;
public InputData(InputStream input)
{
this.input=new BufferedReader(new InputStreamReader(input));
}
public void run()
{
while(true)
{
byte[] b=new byte[2000];
try
{
String str=input.readLine();
if(str.equals("end"))
break;
System.out.println("client: "+str);
}
catch(Exception e)
{
}
}
Flag.f.setFlag(false);
try
{
input.close();
}
catch(Exception e)
{
}
}
}class Flag
{
private boolean flag=true;
public void setFlag(boolean flag)
{
this.flag=flag;
}
public boolean getFlag()
{
return flag;
}
private Flag()
{
}
public static Flag f=new Flag();
}客户端程序:
import java.net.*;
import java.io.*;class TcpClient
{
private BufferedReader read=null;
private OutputStream output=null;
public TcpClient(Socket client)
{
try
{
read=new BufferedReader(new InputStreamReader(System.in));
output=client.getOutputStream();
}
catch(Exception e)
{
System.out.println("error in TcpClient constractor");
}
}
public void writeData()
{
while(true)
{
try
{
String str=read.readLine();
output.write((str+System.getProperty("line.separator")).getBytes());
if(str.equals("end"))
break;
}
catch(Exception e)
{
System.out.println("error in writeData");
}
}
release();
}
public void release()
{
try
{
read.close();
output.close();
}
catch(Exception e)
{
System.out.println("error in release");
}
}
public static void main(String[] agrs)
{
try
{
Socket client=new Socket(InetAddress.getByName("192.168.1.8"),3000);
Thread t=new Thread(new DialogToServer_ReadData(client));
t.setDaemon(true);
t.start();
TcpClient x=new TcpClient(client);
x.writeData();
client.close();
}
catch(Exception e)
{
System.out.println("error in main");
}
}
}class DialogToServer_ReadData implements Runnable
{
private BufferedReader input=null;
public DialogToServer_ReadData(Socket client)
{
try
{
input=new BufferedReader(new InputStreamReader(client.getInputStream()));
}
catch(Exception e)
{
System.out.println("error in DialogToServer_ReadData constractor");
}
}
public void run()
{
while(true)
{
try
{
System.out.println("server: "+input.readLine());
}
catch(Exception e)
{
System.out.println("error in run");
}
}
}
}
可在同一机器上运行,要先运行服务器程序
关闭时,应先关闭客户机程序