Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Mnelson

Thread: TCP Client problems

  1. #1
    Mnelson is offline Member
    Join Date
    Jun 2012
    Rep Power

    Question TCP Client problems

    Hello. I'm trying to figure out why my TCP Client code isnt working. I've built the server end of it also. Unfortunately I get this error when I try to run.

    java TCPAdditionClient
    Exception in thread "main" Connection refused: connect
    at Method)
    at known Source)
    at Source)
    at Unknown Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at<init>(Unknown Source)
    at<init>(Unknown Source)
    at TCPAdditionClient.main(

    Can anyone please let me know what I'm doing wrong here?

    Here is my Client code:
    Java Code:
    		String sentence;
    		  String modifiedSentence;
    		  BufferedReader inFromUser = new BufferedReader( new InputStreamReader(;
    		  Socket clientSocket = new Socket("localhost", 4567);
    		  DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    		  BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    		  sentence = inFromUser.readLine();
    		  outToServer.writeBytes(sentence + '\n');
    		  modifiedSentence = inFromServer.readLine();
    		  System.out.println("FROM SERVER: " + modifiedSentence);
    Here is my server code:
    Java Code:
    int open = 1; int var= 0;
    		String clientSentence; String capitalizedSentence; 
    		ServerSocket welcomeSocket = new ServerSocket(var);
    //Checks presence of argument
    	    if(args.length !=0){
    	        	var = (Integer.valueOf(args[0]));
    //Throws Exception if Argument is not present
    	    if(args.length == 0){
    	    	throw new IllegalArgumentException("You need to specify a port number when executing program...  Example: \"java IntCountServer 1234\" Please try again");
    //Accepts and reads input from client	    
    	    while(open == 1)
               Socket connectionSocket = welcomeSocket.accept();
               BufferedReader inFromClient =
                  new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
               DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
               clientSentence = inFromClient.readLine();
               System.out.println("Data received from client: " + clientSentence);
               capitalizedSentence = clientSentence.toUpperCase() + '\n';

  2. #2
    Mnelson is offline Member
    Join Date
    Jun 2012
    Rep Power

Similar Threads

  1. server/client problems
    By invidos in forum Advanced Java
    Replies: 1
    Last Post: 03-01-2012, 07:11 PM
  2. FTP client problems
    By dankobanana in forum New To Java
    Replies: 4
    Last Post: 02-20-2012, 01:59 AM
  3. Problems with client and server
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:07 AM
  4. Problems in running client class
    By ai_2007 in forum Advanced Java
    Replies: 0
    Last Post: 06-30-2007, 02:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts