端口连接不起作用

时间:2017-08-13 05:58:56

标签: java sockets http server client

实现一个java程序,用于为网页上传和下载创建HTTP套接字。我写了一个服务器端程序和客户端程序。当我运行该程序时,它说原生端口问题。

Server.Java

package javaapplication1;
import java.io.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;

public class Server {



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


    ServerSocket server= null;
    Socket socket;
    server=new ServerSocket(4000);
    System.out.println("Server waiting for image");
    socket=server.accept();
    System.out.println("Client Connected");
    InputStream in=socket.getInputStream();
    DataInputStream dis=new DataInputStream(in);
    int len=dis.readInt();
    System.out.println("Image size:"+len/1024+"kb");
    byte[] data=new byte[len];
    dis.readFully(data);
    dis.close();
    in.close();
    InputStream ian=new ByteArrayInputStream(data);
    BufferedImage bImage=ImageIO.read(ian);
    JFrame f=new JFrame("server");
    ImageIcon icon=new ImageIcon(bImage);
    JLabel l=new JLabel();
    l.setIcon(icon);
    f.add(l);
    f.pack();
    f.setVisible(true);



    }

}

Client.Java

package javaapplication1;
import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
 *
 * @author Mohamed
 */
public class Client {


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


      Socket soc;
      BufferedImage img= null;
      soc=new Socket("localhost",4000);
      System.out.println("Client running");
       try

         {
                System.out.println("Reading image from disk");
        img=ImageIO.read(new File("blue.jpg"));
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        ImageIO.write(img,"jpg",baos);
        baos.flush();
        byte[] bytes=baos.toByteArray();
        baos.close();
        System.out.println("Sending image to server");
        OutputStream out=soc.getOutputStream();
        DataOutputStream dos=new DataOutputStream(out);
        dos.writeInt(bytes.length);
        dos.write(bytes,0,bytes.length);
        System.out.println("Image sent to server");
        dos.close();
        out.close();
    }
    catch(Exception e)
    {
        System.out.println("Exception:"+e.getMessage());
        soc.close();
    }
     soc.close();
    }
}

Client.Java的错误位于Error message for the code Client.Java

下面的图片中

在命令提示符中执行代码时实际输出的内容如下

C:\Program Files\Java\jdk.1.6.0_21\bin>javac Server.java
C:\Program Files\Java\jdk.1.6.0_21\bin>java Server
Server waiting for image
Client connected
Image size:27kb

C:\Program Files\Java\jdk.1.6.0_21\bin>javac Client.java
C:\Program Files\Java\jdk.1.6.0_21\bin>java Client
Client is running
Reading image from disk
Sending Image to server
Image sent to the server 
<And the image is displayed>

请告诉我错误是什么或留下正确代码的评论。谢谢!

1 个答案:

答案 0 :(得分:-2)

当客户端无法访问ServerSocket时,会发生Connection拒绝。 您可能需要查看此内容:java.net.ConnectException: Connection refused

使用您的代码,我没有看到任何错误。