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

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 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池
  • ¥15 服务器打印水晶报表问题
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题