Thursday, 15 January 2015

java - MultiThreaded Client Server Socket Program -



java - MultiThreaded Client Server Socket Program -

i wrote first multi threaded client server program. not working expected. couldn't understand did wrong here. can guide me this?

my server code:

public class server { private static final int port_number = 19878; public static void main(string args[]){ serversocket myservice; seek { service = new serversocket(port_number); system.out.println("server started"); while (true) { system.out.println("connection established"); socket request= myservice.accept(); system.out.println("accepted connection : " + request.getport()); connectionhandler handler = new connectionhandler(request); thread serverthread = new thread(handler); serverthread.start(); } } grab (exception e) { e.printstacktrace(); system.out.println("connection error"); } } }

my server thread:

public class connectionhandler implements runnable{ private socket request; private string inputtext; private string outputtext= "hi client"; //inetaddress address; public connectionhandler(socket request) { super(); this.request= request; } @override public void run() { // todo auto-generated method stub seek { bufferedreader input = new bufferedreader(new inputstreamreader(channel.getinputstream())); bufferedwriter output = new bufferedwriter(new outputstreamwriter(channel.getoutputstream())); inputtext= input.readline(); if (inputtext.compareto("end")!=0 || inputtext!= null ){ system.out.println("received message client: " + inputtext); output.write(outputtext); system.out.println("message client server : " + outputtext); } else { output.flush(); output.close(); input.close(); channel.close(); } }catch (ioexception e) { // todo auto-generated grab block e.printstacktrace(); system.out.println("connection error"); } } }

my client code:

public class client { public static integer serverhost = 19878; public static string machinename; public static string inputtext; public static string outputtext= "hi server!!"; public static void main(string args[]){ socket request; seek { request = new socket(machinename,serverhost); system.out.println("sending request server"); bufferedreader input = new bufferedreader(new inputstreamreader(request.getinputstream())); bufferedwriter output = new bufferedwriter(new outputstreamwriter(request.getoutputstream())); output.write(outputtext); system.out.println("message server client : " + outputtext); inputtext= input.readline(); system.out.println("received message server : " + inputtext); } grab (exception e) { e.printstacktrace(); system.out.println("connection error"); } } }

what doing wrong here?

when run code getting output(in server side) as:

server started connection established accepted connection : 64236

output(client side):

sending request server message server client : hi server!!

nothing more. , stops there. no reaction after that. server not receiving message .

you said client not sending message server. can please seek ?

output.write(outputtext);

change below code , see if helps.

output.write(outputtext); output.write("\n");`

java multithreading sockets

No comments:

Post a Comment