View RSS Feed

maddy14

tcp client sometimes sends data to server properly and sometimes doesnt send any data

Rate this Entry
by , 07-10-2014 at 09:07 AM (3990 Views)
Quote Originally Posted by maddy14 View Post
Hey thanks a lot guys for your help.Now The program is working but there is an issue.Sometimes the clients sends data perfectly to the server whereas other times the client doesnt send any data to server.I dont know why is it happening .Please help me with this issue

Client
Java Code:
public class Client {
	public static void main(String args[]) throws IOException
	{
		int userinput =1;
		while(userinput==1)
		{
			String serverName = "192.168.0.8";
			int port = 4003;
			try
			{
				System.out.println("Connecting to " + serverName
						+ " on port " + port);
				Socket client = new Socket(serverName, port);
				System.out.println("Just connected to "
						+ client.getRemoteSocketAddress());
				OutputStream outToServer = client.getOutputStream();
				DataOutputStream out =
						new DataOutputStream(outToServer);
				System.out.println("above out.wirte()");
				out.write("any msg".getBytes(),0,"any msg".length());
				out.flush();
				outToServer.flush();
				InputStream inFromServer = client.getInputStream();
				DataInputStream in =
						new DataInputStream(inFromServer);
				ByteArrayOutputStream buffer = new ByteArrayOutputStream();

				int nRead;
				//byte[] data = new byte[16384];
				//in.read(data);
				System.out.println("converting array "+in);

				byte[] data = IOUtils.toByteArray(in);
				String msgfromserver = new String(data);
				System.out.println("mstg from server "+msgfromserver);
				in.close();
				out.close();

				inFromServer.close();
				outToServer.close();


				client.close();
			}catch(IOException e)
			{ 
				e.printStackTrace();
			}
			System.out.println("Enter userinput ");
			DataInputStream dis = new DataInputStream(System.in);
			String s = dis.readLine();
			userinput = Integer.parseInt(s);
		}
	}

}
Server

Java Code:
public class Server extends Thread
{
	private ServerSocket serverSocket;

	public Server(int port) throws IOException
	{
		serverSocket = new ServerSocket(port);
		//serverSocket.setSoTimeout(100000);
	}

	public void run()
	{
		while(true)
		{
			try
			{
				System.out.println("Waiting for client on port " +
						serverSocket.getLocalPort() + "...");
				Socket server = serverSocket.accept();
				System.out.println("Just connected to "
						+ server.getRemoteSocketAddress());
				byte Message[]=null;
				DataInputStream in =
						new DataInputStream(server.getInputStream());

				System.out.println(server.getInputStream());
				ByteArrayOutputStream buffer = new ByteArrayOutputStream();

				int nRead=0;
				byte[] data = new byte[in.available()];
				System.out.println("outside while loop");
				in.read(data);
				/*while (nRead < in.available()) {
					buffer.write(data, 0, nRead);
					nRead++;
				}*/
				System.out.println("completed whie loop");
				/*buffer.flush();

				data= buffer.toByteArray();*/
				
				/*  for (int i=0;i<length;i++) {
        	   {     
        	    byte temp= in.readByte();
        	    Message[i]=temp;
        	   }
        	  }*/
				System.out.println(data);
				
				String convertmsg = new String(data);
				System.out.println("Msg converted "+convertmsg);
				/*String tempoutput ="firstindex|msgafterwards";//in.readUTF();

				System.out.println(tempoutput);
				int posfirstpipe = tempoutput.indexOf('|');
				tempoutput = tempoutput.substring(0,posfirstpipe);*/
				System.out.println("above dataoutputstream");
				DataOutputStream out =
						new DataOutputStream(server.getOutputStream());
				System.out.println("below dataoutputstream");
				out.write("Success".getBytes(), 0, "Success".length());
				server.getOutputStream().flush();
				server.getInputStream().close();
				//server.getOutputStream().close();
				in.close();
				out.close();
				server.close();
			}catch(SocketTimeoutException s)
			{
				System.out.println("Socket timed out!");
				break;
			}catch(IOException e)
			{
				e.printStackTrace();
				break;
			}
		}
	}
	public static void main(String [] args)
	{
		int port = 4003;// Integer.parseInt(args[0]);
		try
		{
			Thread t = new Server(port);
			t.start();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	}
}

Submit "tcp client sometimes sends data to server properly and sometimes doesnt send any data" to Facebook Submit "tcp client sometimes sends data to server properly and sometimes doesnt send any data" to Digg Submit "tcp client sometimes sends data to server properly and sometimes doesnt send any data" to del.icio.us Submit "tcp client sometimes sends data to server properly and sometimes doesnt send any data" to StumbleUpon Submit "tcp client sometimes sends data to server properly and sometimes doesnt send any data" to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments