1

I am trying to get comfortable with socket programming and wanted to write a program where a client is able to enter a website/port, pass that information to the server, have the server run an HTTP Get for all page text, print it (server side) and then pass that text back to the client for printing (client side).

So far I am able to get it to the point where it reads given web page server side and attempts to input that page into a String but for some reason it passes the FIRST line of said string back to the client and nothing else. I also get this error server side:

Exception in thread "main" java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.writeBytes(Unknown Source)
at assign1.TCPServer15.main(TCPServer15.java:48)

I am not 100% sure what is going wrong and I have tried to troubleshoot a variety of things, and this is my first time trying to do something like this so I'd love tips!

Here is the code for Client

import java.io.*;
import java.net.*;

class TCPWebClient
{
    public static void main(String argv[]) throws Exception
    {
        String sentence;
        String webText;

        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        Socket clientSocket = new Socket("localhost", 6789);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        System.out.print("Enter a single string in the form: server/port. \nExample: (www.google.com/80)\n");
        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + '\n');

        webText = inFromServer.readLine();      
        System.out.println("FROM SERVER:\n" + webText);
        clientSocket.close();
    }
}

And my code for server:

import java.io.*;
import java.net.*;

class TCPServer15
{
   public static void main(String argv[]) throws Exception
       {
         String clientSentence;

         ServerSocket welcomeSocket = new ServerSocket(6789);

         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println("Received: " + clientSentence);

            String [] connectionInfo = clientSentence.split("/");
            Socket webSocket = new Socket(connectionInfo[0], Integer.parseInt(connectionInfo[1]));

            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(webSocket.getOutputStream()))); 
            out.println("GET /index.html HTTP/1.0"); 
            out.println(); 
            out.flush(); 

            BufferedReader inFromWeb = new BufferedReader(new InputStreamReader(webSocket.getInputStream())); 

            String inputLine; 
            StringBuilder stringBuilder = new StringBuilder();

            while ((inputLine = inFromWeb.readLine()) != null) 
            { 
            System.out.println(inputLine); 
            stringBuilder.append(inputLine);
            stringBuilder.append("\n");
            } 

            String finalString = stringBuilder.toString();
            inFromWeb.close(); 

            outToClient.writeBytes(finalString);
         }
      }
}
zr0z
  • 23
  • 1
  • 5

2 Answers2

1

You are reading only one line in your client. this should be in a while loop (check how you have coded your server while it talks to google. the client should behave the same way.

I have modified your client and server - snippets

Server

        outToClient.writeBytes(finalString);
        outToClient.flush();
        outToClient.close();
        inFromWeb.close();

Client

    StringBuilder output = new StringBuilder();
    while ((webText = inFromServer.readLine()) != null) {

        output.append(webText);
        output.append("\n");
    }

    System.out.println("FROM SERVER:\n" + output.toString());

    clientSocket.close();
Ramanan
  • 39
  • 4
0

I was facing the same issue. Commonly This kind of error occurs due to client has closed its connection and server still trying to write on that client. So make sure that your client has its connection open until server done with its outputstream. And one more thing, Don`t forgot to close input and output stream.

Kaushal28
  • 5,377
  • 5
  • 41
  • 72