I am trying to learn network programming in java so far i have written 2 codes, one for client side, one for server side. First i run serverCode and then i run client code however client is recieving null on its end.
Server Side code:
import java.net.*;
import java.io.*;
/**
*
* @author saksham
*/
public class Chatserver {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
ServerSocket serverSocket=new ServerSocket(6966);
while (true){
Socket request=serverSocket.accept();
System.out.println("Connection established");
PrintWriter pw=new PrintWriter(request.getOutputStream());
// InputStreamReader ir=new InputStreamReader(request.getInputStream());
//BufferedReader bf=new BufferedReader(ir);
//String msg=bf.readLine();
pw.println("you sent me the message:");
request.close();
}
}
}
Client side code:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.io.*;
import java.util.Scanner;
/**
*
* @author saksham
*/
public class Chat {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
Socket socket=new Socket("127.0.0.1",6966);
// PrintWriter pw=new PrintWriter(socket.getOutputStream());
//System.out.println("\nEnter a message:->");
//Scanner sc=new Scanner(System.in);
//pw.println(sc.next());
InputStreamReader ir=new InputStreamReader(socket.getInputStream());
BufferedReader br=new BufferedReader(ir);
String rcvd=br.readLine();
socket.close();
System.out.println(rcvd);
rcvd=br.readLine();
System.out.println(rcvd);
}
}
I am very much aware i am not using a good coding style its just a test code since i am focussing on learning for now.