text: :
  Sockets and Internet Protocols
  Not able to use TextIO.putln() method in Networking
  VarunS Singh
  Ranch Hand
  Posts: 38
  posted 1 year ago
  package Chat_App; import java.net.*; import java.io.*; import java.util.*; import java.lang.*; public class Client { static final int DEFAULT_PORT = 1278; //Port where server is listening static final String HANDSHAKE = "START CHAT..."; //End of each message this is send static final char MESSAGE = '0'; // This character is prepended to every message that is sent. static final char CLOSE = '1'; // This character is sent to the connected program when the user quits. public static void main(String[] args) { String computer; // The computer where the server is running,as specified on the command line. //It can be either an IP number or a domain name. int port; // The port on which the server listens. Socket connection; // For communication with the server. TextReader incoming; // Stream for receiving data from server. PrintWriter outgoing; // Stream for sending data to server. String messageOut; // A message to be sent to the server. String messageIn; // A message received from the server. // First, get the computer from the command line.Get the port from the command line, if one is specified, //or use the default port if none is specified. if(args.length==0){ TextIO.putln("Usage: javaSimpleClient<computer-name[<port>]"); return; } computer = args[0]; if(args.length==1){ port = DEFAULT_PORT; }else{ try{ port = Integer.parseInt(args[1]); if(port<=0 || port>65535){ throw new NumberFormatException(); } }catch(NumberFormatException ex){ TextIO.putln("Illegal port number, " + args[1]); return; } } /* Open a connetion to the server. Create streams for communication and exchange the handshake. */ try { Text("Connecting to "+computer+" on port "+port); connection = new Socket(computer,port); incoming = new TextReader(connection.getInputStream()); outgoing = new PrintWriter(connection.getOutputStream()); outgoing.println("handshake.."); outgoing.flush(); messageIn = incoming.getIn(); if(!messageIn.equals(HANDSHAKE)){ throw new IOException("Connected program is not Chat!"); }else{ TextIO.putln("Connected. Enter your first message..."); } } catch (Exception e) { TextIO.putln("An error occurred while opening connection."); TextIO.putln(e.toString()); return; } /* Exchange messages with the other end of the connection until one side or the other closes the connection. This client program send the first message. After that, messages alternate strictly back an forth. */ try{ while(true){ TextIO.put("SEND: "); messageOut = TextIO.getIn(); if (messageOut.equalsIgnoreCase("quit")) { // User wants to quit. Inform the other side // of the connection, then close the connection. outgoing.println(CLOSE); outgoing.flush(); connection.close(); TextIO.putln("Connection closed.."); break; } } outgoing.println(MESSAGE + messageOut); outgoing.flush(); if (outgoing.checkError()) { throw new IOException("Error ocurred while reading incoming message."); } TextIO.putln("WAITING..."); messageIn = incoming.getln(); if (messageIn.length() > 0) { // The first character of the message is a command. // If the command is CLOSE, then the connection // is closed. Otherwise, remove the command // character from the message and procede. if (messageIn.charAt(0) == CLOSE) { TextIO.putln("Connection closed at other end."); connection.close(); break; } messageIn = messageIn.substring(1); } TextIO.putln("RECEIVED: " + messageIn); }catch (Exception e) { TextIO.putln("Sorry, an error has occurred. Connection lost."); TextIO.putln(e.toString()); System.exit(1); } } }
  Above shown is the code for Client side in networking and the problem is that I am not being able to know why the method TextIO.putln() is not working?
  Thanks in advance for any suggestions
  Stephan van Hulst
  Saloon Keeper
  Posts: 9799
  197
  posted 1 year ago
  What is
  TextIO
  ? It's not part of
  Java
  's standard API, and you have no import for it, so either you wrote it yourself or you forgot to import it.
  You really should break up your
  main()
  method. It does way way too much, and it's poorly readable.
  Did you see how Paul
  cut 87% off of his electric heat bill with 82 watts of micro heaters
  ?
  Post Reply
  Bookmark Topic
  Watch Topic
  New Topic
  Boost this thread!
  Similar Threads
  Can Someone tell me what I am doing wrong??
  Java My Interview Question
  simple simple chat server
  Can't catch Window Close event when trying SocketServer trying to accept connections
  How to send image over a network
  More...