Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help: Java socket streams do not work

stream

  • Please log in to reply
6 replies to this topic

#1 mikeraul

mikeraul

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 23 July 2009 - 08:21 AM

Hello,
I've been trying to write a simple server-client application in java in which the client sends the number 1 to the server, and the server sends back the following number until the number reaches 21.
For example,
Client sends 1
Server sends 2
Client sends 3
..... 21
However, the program seems to stop after the client sends the number 1.
import java.io.*;
import java.net.*;
import java.util.*;

public class Server {
	static Socket link = null;
	public static void main(String[] args){
	int num = 0;
	ServerSocket servSock = null;
	 try {
		servSock= new ServerSocket(1234);
	} catch (IOException e) {
		System.out.println("Cannot bind to port ");
		System.exit(1);
	}
	
	try {
		link = servSock.accept();
	} catch (IOException e) {
		e.printStackTrace();
	}
	System.out.println("established connections");
	Scanner  input = null;
	try {
		input = new Scanner(link.getInputStream());
	} catch (IOException e) {
		
		e.printStackTrace();
	}
	PrintWriter output = null;
	try {
		output = new PrintWriter(link.getOutputStream(),true);
	} catch (IOException e) {
		e.printStackTrace();
	}
	
	while(input.nextInt()!=-1){
	num=input.nextInt();
	System.out.println("Server recieved " +num+ " from the client");
	output.print(num++);
	System.out.println("Server sent " +(num++)+ "to the client");
	};

		System.out.println("\n* Closing connection... *");
		try {
			link.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	
}

import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
	static Socket link=null;
	public static void main(String[] args){
		int num=0;
		
		try {
			link =new Socket(InetAddress.getLocalHost(),1234);
		} catch (UnknownHostException e) {
			System.out.println("unknown host");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("unable to connect");
			e.printStackTrace();
		}	
		Scanner  input = null;
		try {
			input = new Scanner(link.getInputStream());
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		PrintWriter output = null;
		try {
			output = new PrintWriter(link.getOutputStream(),true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		while (num!=21){
		output.print(num++);
		System.out.println("client sent " +(num++)+ "to the Server");
		num=input.nextInt();
		System.out.println("client recieved " +num+ " from the   client");
		}
			output.print(-1);
		try {
			link.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("  client connection closed");
	}
}


I wish someone would be able to tell me what is the problem.

Thanks in advance.
  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 23 July 2009 - 11:08 PM

This works:
There is something wrong with the input (with debugging you notice both server and client classes are waiting on the input line)
I changed Scanner it into a bufferedreader and it goes fine. as i use input.readline, i have also changed output.print(), into output.println()

and ofc, as i read strings now, added a Integer.parseInt(string);

(maybe i changed some other unnecessarythings :P)

import java.io.*;
import java.net.*;
import java.util.*;

public class Server {
	static Socket link = null;
	public static void main(String[] args){
	int num=0;
	ServerSocket servSock = null;
    String string;
	 try {
		servSock= new ServerSocket(9234);
	} catch (IOException e) {
		System.out.println("Cannot bind to port ");
		System.exit(1);
	}

	try {
		link = servSock.accept();
	} catch (IOException e) {
		e.printStackTrace();
	}
	System.out.println("established connections");
	BufferedReader  input = null;
	try {
		input = new BufferedReader(new InputStreamReader(link.getInputStream()));
	} catch (IOException e) {

		e.printStackTrace();
	}
	PrintWriter output = null;
	try {
		output = new PrintWriter(link.getOutputStream(),true);
	} catch (IOException e) {
		e.printStackTrace();
	}

	do{
        try{
            string = input.readLine();
                num = Integer.parseInt(string);
            }
            catch(IOException e){
                System.out.println("foutfout");
            }
	System.out.println("Server recieved " +num+ " from the client");
	output.println(""+num);
	System.out.println("Server sent " +(num++)+ "to the client");
	}while(num!=-1);

		System.out.println("\n* Closing connection... *");
		try {
			link.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}


}

import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
	static Socket link=null;
	public static void main(String[] args){
		int num=0;
        String string;

		try {
			link =new Socket("localhost",9234);
		} catch (UnknownHostException e) {
			System.out.println("unknown host");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("unable to connect");
			e.printStackTrace();
		}
		BufferedReader  input = null;
		try {
			input = new BufferedReader(new InputStreamReader(link.getInputStream()));
		} catch (IOException e) {

			e.printStackTrace();
		}
		PrintWriter output = null;
		try {
			output = new PrintWriter(link.getOutputStream(),true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		while (num!=21){
        num++;
		output.println(""+num);
		System.out.println("client sent " +num+ " to the Server");
            try{
            string = input.readLine();
                num = Integer.parseInt(string);
            }
            catch(IOException e){
                System.out.println("foutfout");
            }
		System.out.println("client recieved " +num+ " from the   client");
		}
			output.print(-1);
		try {
			link.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("  client connection closed");
	}
}

  • 0

#3 mikeraul

mikeraul

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 23 July 2009 - 11:17 PM

thank you, but why does it work with bufferedReader but not with a scanner?

Edited by mikeraul, 24 July 2009 - 02:12 AM.

  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 24 July 2009 - 02:18 AM

No idea.
To be honest, this was my first Client / server i've ever seen in Java. Most of the code seemed logical and i understood most of it so i gave it a go. With some googling i saw quite some examples of client/server classes in Java and i saw no Scanner anywhere..

It may however be possible with a Scanner. This guy also tried it:
New To Java - Small Server/Client Problem
and the replies he gets don't suggest to replace Scanner. So there probably might be a problem, not with the scanner but the print.. who knows. i'll take a look at it later today.
  • 0

#5 mikeraul

mikeraul

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 24 July 2009 - 02:51 AM

I think I have understood the problem, it has something to do with the lines because when I added " input.nextLine() " after " num=input.nextInt();" and changed all the "output.print" to "output.println" ,just like you did , it worked fine.

I have ecountered some similar problems with Scanner before, so i guess it's a bug or something.



thank you for your effort.
  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 24 July 2009 - 03:41 AM

Weird :confused: There's got to be an explanation for this though.
btw i don't even need to add the input.nextLine() , output.println() is enough.

Server side code
while(num !=-1){  //different while than your code <<----
	num=input.nextInt();
	System.out.println("Server recieved " +num+ " from the client");
	output.println(num);
	System.out.println("Server sent " +num+ "to the client");
	};

		System.out.println("\n* Closing connection... *");
		try {
			link.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
i may have left away some "++" from "num++"
  • 0

#7 mikeraul

mikeraul

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 24 July 2009 - 05:45 AM

Yup works for me too, maybe a Scanner can only read from a new line.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download