Top

MultiThreaded Socket Server


Java Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  0.0 (of 0 scores)
Date Added  Aug 03, 2009
Last Updated  Aug 03, 2009
Tags  client  java  multithread  server  socket  thread 

Introduction

This is a generic socket server. Now, it's not documented very well, but I'll explain things you can change here.

Code:
ServerSocket ssock = new ServerSocket(9899);

Change the 9899 to whichever port you wish this to listen on.

If you look further down, you'll see:
Code:
if ((int) temp == 1) {


Now this is for the server to understand the end of a string (using read() only reads on more char so it's difficult to understand when it's done reading). This (the 1 in the above code) is quite literally the equivalent to $chr(1) for those people who use mIRC.

I personally used NetBeans to compile this.

Notes:
-I've been getting this error message, only recently (after some few days of testing)
Code:
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space

I am working on fixing this.


Grab the Code

 
import java.io.*;
import java.net.*;
 
public class MultiThreadServer implements Runnable {
 
    Socket socket;
 
    MultiThreadServer(Socket csocket) {
        this.socket = csocket;
    }
 
    public static void main(String args[]) throws Exception {
        ServerSocket ssock = new ServerSocket(9899);
        System.out.println("Listening");
        int a = 0;
        while (true) {
            Socket sock = ssock.accept();
            a++;
            InetAddress addr = sock.getInetAddress();
            System.out.println("Connection made to " + addr.getHostName() + " (" + addr.getHostAddress() + ") ID: " + a);
            new Thread(new MultiThreadServer(sock)).start();
        }
    }
 
    public void run() {
        try {
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    socket.getInputStream()));
            StringBuilder input = new StringBuilder();
            char temp;
            PrintStream ps = new PrintStream(socket.getOutputStream());
            while (true) {
                if (!socket.isConnected()) {
                    System.out.println("NOT CONNECTED ANYMORE!!");
                    break;
                }
                input.append((char) in.read());
                //Last character.
                temp = input.charAt(input.length() - 1);
 
                if ((int) temp == 1) {
                    input.setLength(input.length() - 1);
                    System.out.println("INPUT: " + input);
                    if (input.toString().equals("Hello")) {
                        ps.println("Well Hello There");
                    }
                    input = null;
                    input = new StringBuilder();
                }
            }
            in.close();
            ps.close();
            socket.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}
 

Comments

  (1)  RSS
Joshuaxiong1
Comments: 127
 
Java Snippet:  MultiThreaded Socket Server
Posted on Nov 16, 2009 12:15 pm
Does this open a port on your computer so someone from another can connect to your computer using it as a proxy?

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom