Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Socket : MultiClient Server

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I need to make following server in to MultiClient. I'm new in JAVA, so I can not figure it out. I don't even know if it's possible to make it in any easy way, because I tried to look for any possible information or help and I failed, so, if it's not too complicated, if anyone can help to me?
This server is not written by me, I took it from JAVA SUN page.

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi karl, the below program will help you much.

Server:
public class TCPServer{
private ServerSocket server;
private int port = 7777;

public TCPServer() {
try {
server = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
TCPServer example = new TCPServer();
example.handleConnection();
}

public void handleConnection() {
System.out.println("Waiting for client message...");

while (true) {
try {
Socket socket = server.accept();
ConnectionHandler cd = new ConnectionHandler(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


for each client separate Thread will be created

Thread Class: ConnectionHandler

class ConnectionHandler implements Runnable {
private Socket theclient;
ServerSocket serverSocket = null;

public ConnectionHandler(Socket theclient) {
this.theclient = theclient;

Thread t = new Thread(this);
t.start();
}

public void run() {
try
{
PrintWriter out = new PrintWriter(theclient.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(theclient.getInputStream()));
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

String inputLine, outputLine;
while((inputLine=in.readLine()) != null) {
out.println();

System.out.println("From Client:"+inputLine);
outputLine=input.readLine();
out.println(outputLine);

if (inputLine.equals("Bye"))
break;
}
out.flush();

out.close();
in.close();
theclient.close();
serverSocket.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}


Client:

public class TCPClient {
public static Socket clientSocket = null;
public static PrintWriter out = null;
public static BufferedReader in = null;
public static int port=0;
static InetAddress mcAddress=null;

public static void main(String[] args) {
try
{
mcAddress = InetAddress.getByName("228.1.2.3");
port = Integer.parseInt("7777");
clientSocket = new Socket("localhost", port);
out = new PrintWriter(clientSocket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

String userInput;
String inetAddress = clientSocket.getInetAddress().getHostName();
out.println(inetAddress);
while((userInput = input.readLine())!= null)
{
out.println(userInput);
System.out.println("From Server: "+ in.readLine());
out.flush();
}

out.close();
in.close();
input.close();
clientSocket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}


-Prabhakaran
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both these codes suffer from a fundamental flaw: Don't println to a Socket
 
Karl Vaicek
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Prabhakaran, this is really useful!!
 
A wop bop a lu bop a womp bam boom! Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic