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
    Posts
    11
    Rep Power
    0

    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" java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Un known Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress( Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at TCPAdditionClient.main(TCPAdditionClient.java:10)

    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(System.in));
    		  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);
    		  clientSocket.close();
    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';
               outToClient.writeBytes(capitalizedSentence);

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

Similar Threads

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