Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 3.13 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Can not retrieve data from the socket
  2. package server;
  3. import java.net.*;
  4. import java.io.*;
  5. import java.util.Vector;  
  6.  
  7. import com.sun.org.apache.bcel.internal.generic.NEW;  
  8.  
  9. public class Server {      
  10.  
  11. public static void main(String[] args)throws IOException, InstantiationException,
  12. IllegalAccessException {            
  13.  
  14.     ServerSocket servsocket = null;  
  15.     Socket sock = null;  
  16.     byte[] bytebuffer = new byte[512];  
  17.  
  18.     try {
  19.         System.out.println("SERVER IS RUNNING...");
  20.         servsocket = new ServerSocket(28000);
  21.         while(true){
  22.         sock = servsocket.accept();
  23.         System.out.println(servsocket.isBound());
  24.         System.out.println("Port "+servsocket+" Ready!!!");
  25.         System.out.println("Accept connection requests from " + sock);
  26.         System.out.println("From CLIENT "+sock.getInetAddress()+ " and PORT " +  
  27.             sock.getPort());
  28.         ChatThread thread = new ChatThread(sock);  
  29.         System.out.println("Thread is running");
  30.         thread.run();      
  31.  
  32.  
  33.         }
  34.     } catch (IOException ioe) {
  35.         System.out.println(ioe);
  36.     }
  37.     finally{
  38.             try {
  39.                 servsocket.close();
  40.                 } catch (IOException ioe) {
  41.                     System.out.println(ioe);
  42.                 }
  43.             }      
  44.     }
  45.     }
  46.  
  47. class ChatThread extends Thread{
  48.     static Vector<ChatThread> chatthread = new Vector<ChatThread>(10);
  49.     private Socket sock;
  50.     private BufferedReader in ;
  51.     private PrintWriter out;
  52.  
  53.     public ChatThread (Socket socket) throws IOException {
  54.         this.sock = socket;
  55.         in = new BufferedReader(
  56.                 new InputStreamReader(socket.getInputStream()));
  57.         out = new PrintWriter(
  58.                 new OutputStreamWriter(socket.getOutputStream()));
  59.  
  60.         byte[] bytebuffer = new byte[512];
  61.         int receivemssg;
  62.  
  63.     }
  64.  
  65.     public void run(){
  66.         int recvMsgSize;
  67.         byte[] bytebuffer = new byte[512];
  68.         String readsocket;
  69.         try {
  70.             readsocket = in.readLine();
  71.         } catch (IOException e) {
  72.             e.printStackTrace();
  73.         }
  74.             }
  75.                 }
  76.        
  77. public ChatThread (Socket socket) throws IOException {
  78.         this.sock = socket;
  79.         in  = sock.getInputStream();
  80.         out = sock.getOutputStream();
  81.         byte[] bytebuffer = new byte[512];
  82.         int receivemssg;
  83.  
  84.     }
  85.  
  86.     public void run(){
  87.         int recvMsgSize;
  88.         byte[] bytebuffer = new byte[512];
  89.  
  90.         System.out.println("Thread is Running...");
  91.         String masuk = new String(bytebuffer);
  92.  
  93.         System.out.println(bytebuffer);
  94.         System.out.println(in.toString());
  95.         System.out.println("thread successfully executed !!!");
  96.  
  97.         synchronized (chatthread) {
  98.             chatthread.addElement(this);
  99.         }
  100.  
  101.          try {
  102.             while ((recvMsgSize = in.read(bytebuffer)) != -1) {
  103.                     out.write(bytebuffer, 0, recvMsgSize);
  104.                     System.out.println("The length of a character is received and returned "+bytebuffer.length);
  105.                  }
  106.         } catch (IOException e) {
  107.  
  108.             System.out.println(e);
  109.         }
  110.                     }
  111.                     }