socket通信发生了CONNECTION REFUSED 怎么办

package exercise;
import java.io.*;
import java.net.*;
import java.util.*;
public class clientSocket {
public clientSocket() {
try {
InetAddress ia = InetAddress.getByName("127.0.0.1");
Socket soc = new Socket(ia,8000);
InputStream is = soc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
InputStreamReader user_isr = new InputStreamReader(System.in);
BufferedReader user_br = new BufferedReader(user_isr);
BufferedReader br = new BufferedReader(isr);
OutputStream os = soc.getOutputStream();
PrintStream ps = new PrintStream(os);
System.out.println("这里是客户端: ");
String s;
while(true) {
System.out.println("输入你想要的文件: ");
ps.println(user_br.readLine());
if(user_br.readLine().equals("Bye")) {
break;
}
}
soc.close();
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
clientSocket cs = new clientSocket();
}
}
package exercise;
import java.net.*;
import java.io.*;
import java.util.*;
public class serverSocket {
public serverSocket() {
try {
ServerSocket ss = new ServerSocket(8000);
Socket soc = ss.accept();
InputStream is = soc.getInputStream();
OutputStream os = soc.getOutputStream();
PrintStream ps = new PrintStream(os);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String s1;
System.out.println("服务器端: ");
while(true) {

s1 = br.readLine();

if(s1.equals("Bye")) {
break;
}
File f = new File(s1);
if(!f.exists()) {
ps.println("文件不存在");
}
else if(f.exists()) {
FileInputStream fis = new FileInputStream(s1);
InputStreamReader ipsr = new InputStreamReader(fis);
BufferedReader bfr = new BufferedReader(ipsr);
ps.println(bfr.readLine());
System.out.println("文件内容已发送");
}
}
soc.close();
ss.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args) {
serverSocket SS = new serverSocket();
}
}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐