Exception in thread "main" java.net.ConnectException: Connection refused: connect

Exception in thread "main" java.net.ConnectException: Connection refused: connect,第1张

你是不是贴错了
ServerSocket是服务器端,Socket是客户端
程序中有错误,你试试下面的代码看行不行
服务器端
ServerSocket server = new ServerSocket(1044);
Socket s = serveraccept();//等待客户端连接
BufferedReader br=new BufferedReader(new InputStream( sgetInputStream()));
File f=new File("testtmp");
BufferedWriter bw=new BufferedWriter(new FileWriter(f));
String str=brreadLine();
while(str!=null)
{bwwrite(str);
bwclose();
brclose();
}
Systemoutprintln("已接收到的信息,并保存在临时文件中
");
客户端:
BufferedReader br=new BufferedReader(new FileReader(filename));//filename为文件对象
String serverIP = "127001";//服务器IP地址
Systemoutprintln("正在连接服务器,请稍候");
Socket s = new Socket(serverIP,1044); Systemoutprintln("已正确连接到显示服务端,开始传输信息");
BufferedWriter bw=new BufferedWriter(new OutputStream(new sgetOutputStream()));
代码不是太完整,也未编译。你可以参照其中部分,再对你的程序进行修改,希望对你有所帮助!

客服端:
package MyKeFudaun;
import javaioBufferedReader;
import javaioBufferedWriter;
import javaioIOException;
import javaioInputStreamReader;
import javaioOutputStreamWriter;
import javanetSocket;
import javanetUnknownHostException;
public class KeFuDuan {
public static void main(String[] args) {
KeFuDuan kf = new KeFuDuan();
kfstart();
}
public void start(){
Socket sco;
String ss= "";
try {
sco = new Socket("127001",8866);
KeFuduanJie kf = new KeFuduanJie(sco);
KeFuWuFasong kfs = new KeFuWuFasong(sco);
kfstart();
kfsstart();

//scoclose();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
eprintStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
class KeFuduanJie extends Thread{

Socket soc;
String ss;
BufferedReader br;
public KeFuduanJie(Socket soc){
try {
thissoc = soc;
br = new BufferedReader(new InputStreamReader(socgetInputStream()));

} catch (IOException e) {
eprintStackTrace();
}

}

//负责接受服务端来的信息
public void run(){
while(true){
//接受服务器端来的信息
try {
ss = brreadLine();
Systemoutprintln("服务器---->客服端: "+ss);
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}

}
}
//向服务器发送东西
class KeFuWuFasong extends Thread{

Socket soc;
BufferedWriter bw;
BufferedReader brr;
public KeFuWuFasong(Socket soc){

thissoc = soc;

try {
brr =new BufferedReader(new InputStreamReader(Systemin));
bw = new BufferedWriter(new OutputStreamWriter(socgetOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}

public void run(){
while(true){
//向服务器发送请求
try {
bwwrite(brrreadLine());
bwnewLine();
bwflush();// 或者用bwclose()
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
}
}
服务器端:
package MyKeFudaun;
import javaioBufferedReader;
import javaioBufferedWriter;
import javaioIOException;
import javaioInputStreamReader;
import javaioOutputStreamWriter;
import javanetServerSocket;
import javanetSocket;
public class Server {
public static void main(String[] args) {
Server server = new Server();
serverstart();
}

public void start(){

try { //服务器端打开端口
ServerSocket server = new ServerSocket(4499);
Socket socket = null;
ServerToClientThread stct = null;

while(true){
socket = serveraccept(); //迎接(接收)客户端的Socket访问
stct = new ServerToClientThread(socket); //分配一个新线程负责和信赖的Socket沟通
stctstart();
}

} catch (IOException e) {
eprintStackTrace();
}
}
}
package MyKeFudaun;
import javaioBufferedReader;
import javaioIOException;
import javaioInputStreamReader;
import javanetSocket;
public class ServerReceiveFromClient extends Thread{
Socket socket;
BufferedReader br;
String s;

public ServerReceiveFromClient(Socket socket){
thissocket = socket;
try {
br = new BufferedReader(new InputStreamReader(socketgetInputStream()));
} catch (IOException e) {
eprintStackTrace();
}
}

public void run(){

while(true){
try {
s = brreadLine();
Systemoutprintln(socketgetInetAddress()getHostAddress()+"发送了:"+s);

} catch (Exception e) {
eprintStackTrace();
}

}
}
}
package MyKeFudaun;
import javaioBufferedReader;
import javaioBufferedWriter;
import javaioIOException;
import javaioInputStreamReader;
import javaioOutputStreamWriter;
import javanetSocket;
public class ServerToClientThread extends Thread{
Socket socket;
BufferedReader br;
BufferedWriter bw;
String s;

//建立的同时,和客户端的Socket建立输入、输出流
public ServerToClientThread(Socket socket){
thissocket = socket;
try {
bw = new BufferedWriter(new OutputStreamWriter(socketgetOutputStream()));
} catch (IOException e) {
eprintStackTrace();
}

}

public void run(){
ServerReceiveFromClient srfc = new ServerReceiveFromClient(socket);
srfcstart();

while(true){
try {
bwwrite("欢迎光临。");
bwnewLine();
bwflush();
Threadsleep(101000);
} catch (IOException e) {
eprintStackTrace();
} catch (InterruptedException e) {
eprintStackTrace();
}
}
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/13474933.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-08-14
下一篇 2023-08-14

发表评论

登录后才能评论

评论列表(0条)

保存