Results 1 to 2 of 2
  1. #1
    golupant is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default socket programming- server not receiving message from client

    hi Friends,

    I have below two programmes (Client and server). I want that client send a message and it should be received by server. It seems to me that client is sending the message and it is getting received by server but still server is not printing the message.Please help me out to resolve the issue.I am flushing all the streams.

    MyServer Programme-
    import java.io.IOException;
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;

    public class MyServer {

    /**
    * @param args
    */
    public static void main(String[] args)
    {
    // TODO Auto-generated method stub
    try
    {
    ServerSocket s=new ServerSocket(4000);
    while(true)
    {
    Socket incoming=s.accept();
    System.out.println("new client");
    Runnable r=new MyRunnable(incoming);
    Thread t=new Thread(r);
    t.start();
    }
    }
    catch(IOException e)
    {
    System.out.println("Hey exception occured in connection");
    e.printStackTrace();
    }

    }

    }


    class MyRunnable implements Runnable
    {
    public MyRunnable(Socket s)
    {
    client=s;
    }
    public void run()
    {
    try
    {try
    {
    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    PrintWriter out = new PrintWriter(client.getOutputStream(),true);
    boolean done=false;
    while(!done &&(line=in.readLine())!=null)
    {
    System.out.println("hi i m receiving data");
    out.println(line);
    out.flush();
    if(line.trim().equals("BYE"))
    done=true;
    }
    }
    finally
    {
    client.close();
    }
    }
    catch(IOException e)
    {
    System.out.println("Hey exception occured now here");
    e.printStackTrace();
    }
    }
    private Socket client;
    private String line;
    private Scanner in;
    private PrintWriter out;
    }



    My Client Programme

    import java.net.*;
    import java.io.*;
    import java.util.*;


    public class Test {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new MySocket();
    }

    }

    class MySocket
    {
    public MySocket()
    {
    try
    {
    Socket s=new Socket("localhost",4000);
    Scanner read = new Scanner(System.in);
    PrintWriter w=new PrintWriter(s.getOutputStream(),true);
    String line;
    while(true)
    {
    line=read.nextLine();
    w.println(line);
    w.flush();
    }
    }
    catch(IOException e)
    {e.printStackTrace();}
    }
    }


    below is the output I am getting at the server end.

    [root@Virus clint_server]# /soft_install/jdk1.7.0_05/bin/java MyServer
    new client
    hi i m receiving data
    hi i m receiving data


    and below is the input at client end

    [root@Virus clint_server]# /soft_install/jdk1.7.0_05/bin/java Test
    jsahdajs
    sadbash


    Please help me to resolve the issue.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: socket programming- server not receiving message from client

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Need help in socket programming, client side.
    By rushabh in forum Networking
    Replies: 0
    Last Post: 01-20-2011, 07:33 PM
  2. Problem on server side (Socket Programming)
    By ersachinjain in forum Networking
    Replies: 9
    Last Post: 05-06-2010, 04:21 PM
  3. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •