lglglglglglglglglglg 2017-12-03 12:39 采纳率: 0%
浏览 5599
已采纳

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条回答 默认 最新

  • threenewbee 2017-12-03 12:58
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 10月20日

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低