Hello, I am new to java, and I am trying this simple server client program using InputStream and OutputStream.
However when I try to run it, i get this NullPointerException on the client-side.
Here is my code:
Server:
package demoClientServer;
import java.io.*;
import java.net.*;
class server1 {
public static void main(String args[]) {
String data = "Toobie ornaught toobie";
try {
ServerSocket srvr = new ServerSocket(1234);
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
OutputStream out = skt.getOutputStream();
byte[] bt = data.toByteArray();
System.out.print("Sending string: '" + bt + "'\n");
out.write(bt);
out.close();
skt.close();
srvr.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}
Client:
package demoClientServer;
import java.io.*;
import java.net.*;
class client1 {
public static void main(String args[]) {
try {
Socket skt = new Socket("localhost", 1234);
InputStream in = skt.getInputStream();
System.out.print("Received string: '");
byte[] bt = null;
//in.read(bt);
while (in.read(bt) > 0) {
System.out.println(bt.toString()); // Read one line and output it
}
System.out.print("'\n");
in.close();
}
catch(Exception e) {
e.printStackTrace();
System.out.print("Whoops! It didn't work!\n");
}
}
}
Being new to Java, I may be missing something,
Thanks in advance,