简体   繁体   中英

connection error in socket programming

I'm doing a project i my networks class and I'm facing a problem in my code. that is my code is not accepting the connection. in the picture you can see the problem and here's the code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class WebServer {
public static void main(String args[]) {
WebServer ws = new WebServer();
ws.start();
}

protected void start() {
ServerSocket s;
Socket remote;

System.out.println("Webserver starting up on port 8080");

try {

  s = new ServerSocket(8080);

} catch (Exception e) {
  System.out.println("Error: " + e);
  return;
}

  try {
      System.out.println("Waiting for connection ");
     remote = s.accept();
    System.out.println("Connection, sending data. ");
    BufferedReader in = new BufferedReader(new InputStreamReader(
    remote.getInputStream()));
    PrintWriter out = new PrintWriter(remote.getOutputStream());


    String str = ".";
    while (!str.equals("")){
      str = in.readLine();
    }

    out.println("HTTP/1.0 200 OK");
    out.println("Content-Type: text/html");
    out.println("Server: Bot");

    out.println("");

    out.println("<H1>Welcome to the Ultra Mini-WebServer</H2>");
    out.flush();
    remote.close();
  } catch (Exception e) {
    System.out.println("Error: " + e);
  }

  }
  }

I think there is a problem in the accept function? or should i implement also a recv function? What enter image description here I need from the code is to a chat between the client and the web server.

This code sends a jpeg file and works fine for me:

public class FileServer { 
    static BufferedInputStream bis;
    static BufferedOutputStream bos = null;
    static ServerSocket ss = null;
    static Socket s = null;
    static int handler = 0;
    static byte[] buffer= new byte[1024];

    public static void main(String[] args)throws IOException  {
            ss = new ServerSocket(1234);
            System.out.println("Waiting");
            s = ss.accept();


            bis = new BufferedInputStream(new FileInputStream("b.jpg"));
            bos = new BufferedOutputStream(s.getOutputStream( ));
            while((handler= bis.read(buffer))!=-1){
                    bos.write(buffer,0,handler);
                    System.out.println("Sending");
            }
            System.out.println("File Written Successfully");
            bis.close();
            bos.close();

    }  
}

that is my code is not accepting the connection.

Your code is accepting a connection. When I try eg nc localhost 8080 , I get

Webserver starting up on port 8080
Waiting for connection 
Connection, sending data.

So, the problem must be in your client or on the way to the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM