I have to programs: Server and client.
The client would send a string to server. Server uppercase it and sends it back.
I know there is a lot of code on the web on doing this. This is a homework so I am trying to learn what I am doing wrong.
The client has some generated code. Tried to cut out some of it to make it clear.
Img is of the client.
EDIT: before my last code change, I was getting connect denied. Now the client freezes.
EDIT EDIT: Fixed the freeze. Now :Connection timed out: connect
EDIT EDIT EDIT: Looks like it is getting the Connection, but things are not being done.
package server; import java.io.*; import java.net.*; /** * * @author Hokina */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { String InLine; //make string for input ServerSocket ser = null; try { ser = new ServerSocket(11234); //consturt the socket for lisining } catch (IOException e) { System.out.println(e); } Socket cli = null; try { cli = ser.accept(); // Accepts and connects to incoming connections for sending socket PrintWriter out = new PrintWriter(cli.getOutputStream(), true); //makes a stream for input BufferedReader in = new BufferedReader(new InputStreamReader(cli.getInputStream())); //makes a stream for output // Reads in the line, then outputs the line as upper cased. while ((InLine = in.readLine()) !=null) { out.println(InLine.toUpperCase()); } } catch (IOException e) { System.out.println(e); } try { cli.close(); ser.close(); } catch (IOException e) { System.out.println(e); } } } }
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // Clears text fields jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); // TODO add your handling code here: } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //make vars Socket cli = null; BufferedReader read = null; PrintWriter out = null; try { cli = new Socket(jTextField1.getText() , 11234); // sets up connetion to server. out = new PrintWriter(cli.getOutputStream(), true); // sets up stream for output read = new BufferedReader(new InputStreamReader(cli.getInputStream())); } catch (UnknownHostException e) { System.out.println("Failed find hostname"); } catch (IOException e) { System.out.println("Failed I/O" + e); } try { out.write(jTextField3.getText() + '\n'); //send text over connection); } catch (Exception e) { System.out.println("fail" + e); } try { jTextField2.setText(read.readLine()); //trys to read in text form server // closes connection cli.close(); } catch (IOException e) { System.out.println("fail" + e); } try { out.close(); read.close(); cli.close(); } catch (Exception e) { System.out.println(e); } } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } }