sending 1 message to server

Dear all,

I am trying to modify the code from java tutorial website and create a pair of very simple program which send and receive a string. Which mean I have a pair of program, one is Server and one is Client. What is happening is the Server program stop right after a socket is generate. Could anyone please give me some idea. Thanks

//////// Server //////////

package im;

import java.io.*;

import java.net.*;

import javax.swing.JTextField;

import javax.swing.JTextArea;

publicclass Serverimplements Runnable{

private ServerSocket server;

publicint PORT;

public Socket serversocket;

public String String;

public JTextArea ServerTextArea;

public Server(){

this.ServerTextArea = ServerTextArea;

PORT = 9060;

try{

server =new ServerSocket(PORT);

serversocket = server.accept();

System.out.println("Server1");

}catch (IOException e){

System.out.println("Could not listen on port");

System.exit(-1);

}

}

publicvoid ChangePort(int newPort){

PORT = newPort;

}

publicvoid run(){

String line;

BufferedReader in =null;

PrintWriter out =null;

try{

in =new BufferedReader(new

InputStreamReader(serversocket.getInputStream()));

//out = new PrintWriter(serversocket.getOutputStream(), true);

}catch (IOException e){

System.out.println("in or out failed");

System.exit(-1);

}

while(true){

try{

line = in.readLine();

System.out.println(line);

//out.println(line);

//ServerTextArea.append(line);

}catch (IOException e){

System.out.println("Read failed");

System.exit(-1);

}

}

}

publicstaticvoid main(String[] args)throws IOException{

Server Server =new Server();

//Send.SendMessage();

}

}

////////Client//////////////

package im;

import java.io.*;

import java.net.*;

import javax.swing.JTextField;

import javax.swing.JTextArea;

publicclass Client{

public JTextArea ClientTextArea;

public JTextField ClientTextField;

//private String ip;

privateint PORT = 9060;

public Client(){

}

privatevoid SendMessage()throws IOException{

InetAddress ip =

InetAddress.getByName(null);

Socket socket =new Socket(ip, PORT);

System.out.println("Client1");

try{

PrintWriter out =

new PrintWriter(

new BufferedWriter(

new OutputStreamWriter(

socket.getOutputStream())),true);

out.print("KKKKKKKKKKKKKKKKKKK");

}finally{

socket.close();

}

}

publicstaticvoid main(String[] args)throws IOException{

Client Send =new Client();

Send.SendMessage();

}

}

[6316 byte] By [marco_wua] at [2007-11-27 10:16:11]
# 1

try{

line = in.readLine();

System.out.println(line);

//out.println(line);

//ServerTextArea.append(line);

}catch (IOException e) {

System.out.println("Read failed");

System.exit(-1);

}

Don't exit the program if you don't get any input on the server side. The point of running that loop in a thread is that it can keep checking until it actually does get some input. You won't get anything until the client is up and running, which has to come after the server is up and running. So if you quit after the first failed read, you won't give the client a chance to send anything.

hunter9000a at 2007-7-28 15:44:06 > top of Java-index,Java Essentials,Java Programming...
# 2

Your Server is setup to both listen for the socket to be opened (constructor) and to process the data sent to the socket (run() method). You create a Server object which listen for the socket to be open (in the constructor); but, you never call the run() method which reads the input or startup a new thread with the Server object (Runnable) and call the start() method.

jbisha at 2007-7-28 15:44:06 > top of Java-index,Java Essentials,Java Programming...
# 3

you mean I should change the code in this way?

package im;

import java.io.*;

import java.net.*;

import javax.swing.JTextField;

import javax.swing.JTextArea;

public class Server implements Runnable {

private ServerSocket server;

public int PORT;

public Socket serversocket;

public String String;

public JTextArea ServerTextArea;

public Server(){

this.ServerTextArea = ServerTextArea;

PORT = 9060;

try{

server = new ServerSocket(PORT);

serversocket = server.accept();

System.out.println("Server1");

} catch (IOException e) {

System.out.println("Could not listen on port");

System.exit(-1);

}

}

public void ChangePort(int newPort){

PORT = newPort;

}

public void run(){

String line;

BufferedReader in = null;

PrintWriter out = null;

try{

in = new BufferedReader(new

InputStreamReader(serversocket.getInputStream()));

//out = new PrintWriter(serversocket.getOutputStream(), true);

} catch (IOException e) {

System.out.println("in or out failed");

System.exit(-1);

}

while(true){

try{

line = in.readLine();

System.out.println(line);

//out.println(line);

//ServerTextArea.append(line);

}catch (IOException e) {

System.out.println("Read failed");

System.exit(-1);

}

}

}

public static void main(String[] args) throws IOException{

Server Server = new Server();

Server.run();

}

}

marco_wua at 2007-7-28 15:44:06 > top of Java-index,Java Essentials,Java Programming...
# 4

> you mean I should change the code in this way?

Does it work now?

hunter9000a at 2007-7-28 15:44:06 > top of Java-index,Java Essentials,Java Programming...
# 5

I start Server first, then Client

What happen is

Server listening and response to Client

However, Client suppose to pass "KKKKKKKKKKK" to Server, it seems the Server does not receive it, and many "null" message show up in the Server

marco_wua at 2007-7-28 15:44:06 > top of Java-index,Java Essentials,Java Programming...
# 6

How to use threads properly:

http://java.sun.com/docs/books/tutorial/essential/concurrency/runthread.html

hunter9000a at 2007-7-28 15:44:06 > top of Java-index,Java Essentials,Java Programming...
# 7

You should never (almost) call the run() method of a Runnable; use the Runnable in the creation of a Thread and call the start() method.

jbisha at 2007-7-28 15:44:06 > top of Java-index,Java Essentials,Java Programming...
# 8

> How to use threads properly:

> http://java.sun.com/docs/books/tutorial/essential/conc

> urrency/runthread.html

I try that way too, but the server program is still unable to receive the client message

marco_wua at 2007-7-28 15:44:06 > top of Java-index,Java Essentials,Java Programming...
# 9

Here's the networking tutorial that I used to learn how to do server/client communication from:

http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

It shows how to set up the server and client sockets, and how to get them to read/write in the correct order.

hunter9000a at 2007-7-28 15:44:06 > top of Java-index,Java Essentials,Java Programming...