Jump to content

Help: Java socket streams do not work

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
mikeraul

mikeraul

    Newbie

  • Members
  • Pip
  • 4 posts
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.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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");
	}
}


#3
mikeraul

mikeraul

    Newbie

  • Members
  • Pip
  • 4 posts
thank you, but why does it work with bufferedReader but not with a scanner?

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


#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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.

#5
mikeraul

mikeraul

    Newbie

  • Members
  • Pip
  • 4 posts
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.

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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++"

#7
mikeraul

mikeraul

    Newbie

  • Members
  • Pip
  • 4 posts
Yup works for me too, maybe a Scanner can only read from a new line.