- Can not retrieve data from the socket
- package server;
- import java.net.*;
- import java.io.*;
- import java.util.Vector;
- import com.sun.org.apache.bcel.internal.generic.NEW;
- public class Server {
- public static void main(String[] args)throws IOException, InstantiationException,
- IllegalAccessException {
- ServerSocket servsocket = null;
- Socket sock = null;
- byte[] bytebuffer = new byte[512];
- try {
- System.out.println("SERVER IS RUNNING...");
- servsocket = new ServerSocket(28000);
- while(true){
- sock = servsocket.accept();
- System.out.println(servsocket.isBound());
- System.out.println("Port "+servsocket+" Ready!!!");
- System.out.println("Accept connection requests from " + sock);
- System.out.println("From CLIENT "+sock.getInetAddress()+ " and PORT " +
- sock.getPort());
- ChatThread thread = new ChatThread(sock);
- System.out.println("Thread is running");
- thread.run();
- }
- } catch (IOException ioe) {
- System.out.println(ioe);
- }
- finally{
- try {
- servsocket.close();
- } catch (IOException ioe) {
- System.out.println(ioe);
- }
- }
- }
- }
- class ChatThread extends Thread{
- static Vector<ChatThread> chatthread = new Vector<ChatThread>(10);
- private Socket sock;
- private BufferedReader in ;
- private PrintWriter out;
- public ChatThread (Socket socket) throws IOException {
- this.sock = socket;
- in = new BufferedReader(
- new InputStreamReader(socket.getInputStream()));
- out = new PrintWriter(
- new OutputStreamWriter(socket.getOutputStream()));
- byte[] bytebuffer = new byte[512];
- int receivemssg;
- }
- public void run(){
- int recvMsgSize;
- byte[] bytebuffer = new byte[512];
- String readsocket;
- try {
- readsocket = in.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public ChatThread (Socket socket) throws IOException {
- this.sock = socket;
- in = sock.getInputStream();
- out = sock.getOutputStream();
- byte[] bytebuffer = new byte[512];
- int receivemssg;
- }
- public void run(){
- int recvMsgSize;
- byte[] bytebuffer = new byte[512];
- System.out.println("Thread is Running...");
- String masuk = new String(bytebuffer);
- System.out.println(bytebuffer);
- System.out.println(in.toString());
- System.out.println("thread successfully executed !!!");
- synchronized (chatthread) {
- chatthread.addElement(this);
- }
- try {
- while ((recvMsgSize = in.read(bytebuffer)) != -1) {
- out.write(bytebuffer, 0, recvMsgSize);
- System.out.println("The length of a character is received and returned "+bytebuffer.length);
- }
- } catch (IOException e) {
- System.out.println(e);
- }
- }
- }