compiling server-client with eclipse

hi. i'm new to java programming and i'm using eclipse. i have 2 simple programmes, one for a server and another for a client and would like to use them to communicate with each other. i have an error message with both programs.. i think that there is a prblem with my server program...is there anything i need to add to get it running or is there anything in particular needed when using eclipse to run server/client programs? here is the code:

import java.io.*;

import java.net.*;

public class Serveur {

public static void main(String[] args) {

try{

ServerSocket ss= new ServerSocket(1500);

Socket sc= ss.accept();

System.out.println("connection etablie");

PrintWriter flux_sortant = new PrintWriter(sc.getOutputStream());

flux_sortant.println("coucou j'ecris");

//ss.close();

//sc.close();

}

catch (IOException e){

System.out.println("Connection non etablie");

}

}

}

and for the client:

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) {

try{

Socket s= new Socket("local host",1500);

BufferedReader flux_entrant= new BufferedReader(new InputStreamReader(s.getInputStream()));

System.out.println("J'ai lu:" + flux_entrant.readLine());

}

catch (IOException e){

System.out.println("probleme de connection");

}

}

}

thanks

[1483 byte] By [kikkiea] at [2007-11-26 20:19:08]
# 1

Hello,

if you want your server programm to listen for incoming connections all the time, then you shouldn't close your ServerSocket because it is accepting the connections.

I would try something like this:

public class Serveur implements Runnable{

private boolean running = true;

private ServerSocket ss;

public Serveur(){

ss = new ServerSocket(1500);

}

public static void main(String[] args){

new Thread(new Serveur()).start();

}

public void run(){

while(running){

Socket sc = ss.accept();

System.out.println("Connection established");

PrintWriter writer = new PrintWriter(sc.getOutputStream());

writer.println("Blah");

sc.close();

}

}

}

This server would accept a connection send back "Blah" and close the connection to the client.

hope that helps...

greetz

Message was edited by:

n3bul4

n3bul4a at 2007-7-10 0:42:53 > top of Java-index,Java Essentials,Java Programming...
# 2
It's "localhost", not "local host".Socket s = new Socket("localhost", 1500);Remember to close() streams and sockets at both ends.#
duckbilla at 2007-7-10 0:42:53 > top of Java-index,Java Essentials,Java Programming...