File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes JAVA SOCKET PROGRAMMING Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "JAVA SOCKET PROGRAMMING" Watch "JAVA SOCKET PROGRAMMING" New topic
Author

JAVA SOCKET PROGRAMMING

Adi Peyyala
Greenhorn

Joined: Jan 17, 2005
Posts: 6
I tried writing a client-server java program with the on my machine. The program seem to compile perfectly with the exception that when I run the programs the client exits without giving any message. Could someone help me out.

Thanks.

Server:

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

public class Server {

public static void main(String[] args) {

ServerSocket serverSocket = null;
String line;
BufferedReader is;
PrintStream os;
Socket clientSocket = null;

try {
serverSocket = new ServerSocket(8082);
}

catch(IOException e) {
System.out.println(e);
}

try {
clientSocket = serverSocket.accept();
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
os = new PrintStream(clientSocket.getOutputStream());

while(true) {
line = is.readLine();
os.println(line);
}
}


catch(IOException e) {
System.out.println(e);
}
}
}


Client:

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

public class Client {

public static void main(String[] args) {

Socket clientSocket = null;
DataOutputStream os = null;
BufferedReader is = null;

if(clientSocket != null && os != null && is!= null) {

try {
try {

clientSocket = new Socket("localhost", 8082);
os = new DataOutputStream(clientSocket.getOutputStream());
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch(UnknownHostException e) {
System.err.println("Unknown host name");
} catch(IOException e) {
System.err.println("I/O Connection failure");
}

os.writeBytes("HELLO\n");

String responseLine;
while((responseLine = is.readLine()) != null) {

System.out.println("Server:" + responseLine);
}

os.close();
is.close();
clientSocket.close();

} catch(UnknownHostException e) {
System.err.println("Unknown host name");
} catch(IOException e) {
System.err.println("I/O Connection failure");
}
}
}
}
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093


If your socket, stream or reader are not initialised you don't initialise them but exit the appliction.
As they're declared uninitialised on the line directly prior to your conditional they'll always be uninitialised in the conditional and your application will never do much of anything at all except wonder why it's ready so quickly and terminate in utter confusion.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAVA SOCKET PROGRAMMING
 
Similar Threads
reading from TCP socket problem
Address already used problem in Java Socket application
port number
simple socket programming
Exception in Socket Server