Using getInputStream in a String in a Network Connection in Java

Asked 1 years ago, Updated 1 years ago, 368 views

  • The code below allows you to answer only ○ problems (oh and X).
  • I would like to create a description problem, but I would like to know how to use getInputStream read.
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {

    public static void main(String[]args) {
        // TODO AUTOMATICALLY GENERATED METHOD STUB
        System.out.println("boot complete";
        ServerSocket svSock=null;
        Socket socket = null;
        BufferedWriter w=null;
        try{
            svSock = new ServerSocket (300);
            sock=svSock.accept();
            System.out.println(sock.getInetAddress()+"Connect from");
            sock.getOutputStream().write("Start Quiz\r\n".getBytes("SJIS"));
            //sock.getOutputStream().write("\n".getBytes("SJIS"));
            sock.getOutputStream().write("Is Java the compiler language?\r\n".getBytes("SJIS"));
            sock.getOutputStream().flush();
            intans=sock.getInputStream().read();
            if(String.valueOf((char)ans).equals("o")}
                sock.getOutputStream().write("\r\ncorrect".getBytes("SJIS"));
            } else {
                sock.getOutputStream().write("\r\nIncorrect".getBytes("SJIS"));
            }

        } catch(IOExceptione){
            // TODO Auto-Generated Catch Blocks
            e.printStackTrace();
        }finally {
            try{
                sock.close();
            } catch(IOExceptione){
                // TODO Auto-Generated Catch Blocks
                e.printStackTrace();
            }
        }

    }

}

java

2022-09-30 21:56

1 Answers

Using BufferedReader and BufferedReader and Socket#getInputStream as in SO's main Socket#getInputStreamYou can load it.

Server-side sample code (extracted from the Main class in your question)

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

public class Main {
    public static void main(String[]args) {
        try(ServerSocket svSock=newServerSocket(300);
             Socket socket=svSock.accept();
             // Load input using buffer
             BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream())){
            // Load data up to line feed code
            System.out.println(in.readLine());
        } catch(IOExceptione){
            e.printStackTrace();
        }
    }
}

client-side sample code

import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class Sender {
    public static void main(String[]args) {
        try(Socket socket=new Socket("localhost",300);
             OutputStream output=socket.getOutputStream();
             // Encoding should be changed to anything
             OutputStreamWriter writer = new OutputStreamWriter(output, "Shift-JIS");
             InputStream input=socket.getInputStream()){
            Strings="Hello, socket!\n";
            for(char:s.toCharArray()) {
                writer.write(ch);
            }
            writer.flush();
            // Wait for server processing appropriately
            intc;
            while((c=input.read())!=-1){}
        } catch(Exceptione){
            System.out.println("occurred Exception.");
        }
    }
}


2022-09-30 21:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.