netbeans怎么修改运行服务器,如何在netbeans telnet上运行客户端和服务器代码

这是 link ,我跟着写了一个使用telnet的Java / Netbeans中的简单程序,但我一直得到同样的错误 . 我在这里做错了什么?我刚刚为每个人制作了2个.java文件,并且我试图在net beans中运行它按下运行1

线程“main”中的异常java.net.ConnectException:连接被拒绝:连接

Telnet客户端Telnet客户端//此代码连接到telnet服务器,并在输入命令“1”时接收数据

import java.net.*;

import java.io.*;

class TelnetClient

{

public static void main(String args[]) throws Exception

{

//Create object of Socket

Socket soc=new Socket("localhost",8088);

String Command;

//Create object of Input Stream to read from socket

DataInputStream din=new DataInputStream(soc.getInputStream());

//Create object of Output Stream to write on socket

DataOutputStream dout=new DataOutputStream(soc.getOutputStream());

// Object of Buffered Reader to read command from terminal

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Welcome to Telnet Client");

System.out.println("< Telnet Prompt >");

Command=br.readLine();//reads the command

dout.writeUTF(Command);//sends command to server

System.out.println(din.readLine()); //gets the response of server

soc.close(); //close port

din.close(); //close input stream

dout.close(); //close output stream

br.close(); //close buffered Reader

}

}

Telnet Server //此代码在客户端连接时将字符串发送到客户端//在收到命令“1”时将响应发送到客户端

import java.lang.*;

import java.io.*;

import java.net.*;

class TelnetServer {

public static void main(String args[]) {

String data = "Hello Client!! ";

try {

// Create object of Server Socket

ServerSocket srvr = new ServerSocket(8088);

// Socket object that listens the port (8088) and accepts the incoming connection

//requests

Socket skt = srvr.accept();

System.out.println("Client Connected!");

// gets output stream object

PrintWriter out = new PrintWriter(skt.getOutputStream(), true);

//gets input stream object

DataInputStream din=new DataInputStream(skt.getInputStream());

if(din.readUTF().equals("1")){

// sends response to incoming request if command is '1'

System.out.println("String: '" + data);

out.print(data);

}

out.close();// clos out

skt.close();// close skt

srvr.close();// close srvr

din.close(); // close din

}

catch(Exception e) {

System.out.print(e);

}

}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值