Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Socket problem

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Socket problem

    Hi I wanted to create a client side socket which connects to the host and sends data.
    But the problem is the client connects to it, but doesn't send data.Here's the code:
    import java.net.*;
    import java.io.*;
    import java.rmi.UnknownHostException;
       class Client{
    	   public static void main(String args[]){
    		   Socket sock;
    		   BufferedReader br;
    		   PrintWriter pr;
    		   String str;
    		   try {
     
    	            sock = new Socket("localhost", 80);
    	            System.out.println("Connected to server");
     
     
    	            br = new BufferedReader(new InputStreamReader(
    	            		                sock.getInputStream()));
     
    	            while ((str = br.readLine())!= null) {
    	                System.out.println(str);
     
     
    		   }
    	            System.out.println("Reading complete!");
    	            pr = new PrintWriter(new OutputStreamWriter(
                            sock.getOutputStream()));
     
    			pr.println("Hello.");
    			System.out.println("Message sent");
     
    	            pr.close();
    	            br.close();
    	            sock.close();
    		   }catch (UnknownHostException e) {
    			   System.out.println("Error");
    		   }
     
     
    		   catch(IOException e){
    			   System.out.println("Error");
     
    		   }
    		 }
           }
    Last edited by Robtn; June 3rd, 2011 at 10:56 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket problem

    What does your code do?
    Does it print out anything?
    Does it block/hang waiting for a response from the server?

    Does the server get anything?

    What kind of server are you using?

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Socket problem

    Quote Originally Posted by Norm View Post
    What does your code do?
    Does it print out anything?
    Does it block/hang waiting for a response from the server?

    Does the server get anything?

    What kind of server are you using?
    It prints out texts and player's stats.
    Yes it does.
    Yes it takes.
    The server is multi player game server.port 80

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket problem

    Great, sounds like its working. You have not given enough info for anyone to help you with your problem.

    Can you explain what the problem is and show println output from your code that shows what it does?

    doesn't send data
    how do you know? What debugging steps have you taken to show what is happening on the client side and on the server side? Show the println output and the code that generated it.

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Socket problem

    Quote Originally Posted by Norm View Post
    Great, sounds like its working. You have not given enough info for anyone to help you with your problem.

    Can you explain what the problem is and show println output from your code that shows what it does?


    how do you know? What debugging steps have you taken to show what is happening on the client side and on the server side? Show the println output and the code that generated it.
    sorry i can't speak English well,but i just wanted to send data to the server it's just not working.
    One expert told me that i am putting everything in one function.Can anyone please help please.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket problem

    Before you can communicate with a server, you need to know how the server works.
    What protocol does the server understand? for example: HTTP and FTP are two common protocols.
    Your code connects and then tries to read something.
    What does it read? Is anything printed out?
    Why do you think the server will send you data after you connect to it?
    your program tries to read something from the server. Does the server write anything when it connects?

  7. #7
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Socket problem

    Quote Originally Posted by Norm View Post
    Before you can communicate with a server, you need to know how the server works.
    What protocol does the server understand? for example: HTTP and FTP are two common protocols.
    Your code connects and then tries to read something.
    What does it read? Is anything printed out?
    Why do you think the server will send you data after you connect to it?
    your program tries to read something from the server. Does the server write anything when it connects?
    Very very sorry for the late message.I've been working on other project.
    It reads players stats and chat messages and commands.It also reads in gameplay data.Also it is HTTP server.
    Last edited by Robtn; June 15th, 2011 at 10:50 AM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket problem

    What is the projects status now?

  9. #9
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Socket problem

    What do you mean?I can't speak english much

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket problem

    Do you have a problem now?

  11. #11
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Socket problem

    Quote Originally Posted by Norm View Post
    Do you have a problem now?
    Yes still,it just connects to the server but does nothing.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket problem

    Why do you think the server will send you data after you connect to it?
    Your program tries to read something from the server.
    Does the server write anything when your program connects to it?

Similar Threads

  1. [SOLVED] Problem in socket programming
    By sinni in forum Java Networking
    Replies: 1
    Last Post: March 15th, 2011, 10:26 AM
  2. Replies: 0
    Last Post: February 24th, 2011, 06:31 AM
  3. Problem reading from server socket
    By glauber in forum Java Networking
    Replies: 0
    Last Post: February 15th, 2011, 12:54 PM
  4. problem with closing connection to client socket
    By sunitha in forum Java Networking
    Replies: 1
    Last Post: December 11th, 2010, 04:28 AM
  5. [SOLVED] Socket Programming Problem
    By relixus in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 9th, 2010, 10:35 PM