Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Server -Client Connection

$
0
0
Hello everyone,
I am trying to make a java application in which there are two classes -Server and Client. But these are not connecting.Here are the codes :
package javaapplication5;

import java.io.*;
import java.net.*;
public class SimpleServer
{
    public static void main(String args[])
    {
        ServerSocket s=null;
        try
        {
            s=new ServerSocket (8989);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        while(true)
        {
            try
            {
                Socket s1=s.accept();
                OutputStream s1out=s1.getOutputStream();
                DataOutputStream dos = new DataOutputStream(s1out);
dos.writeUTF("hello");
dos.flush(); // dont forget to call this method 
dos.close();
                s1.close();
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}



and SimpleClient.java :
package javaapplication5;

import java.net.*;
import java.io.*;
public class SimpleClient 
{
    public static void main(String args[])
    {
        try
        {
            Socket s1=new Socket("localhost",8989);
            InputStream is=s1.getInputStream();
            DataInputStream dis=new DataInputStream(is);
            System.out.println(dis.readUTF());
           // br.close();
            s1.close();
        }
        catch(ConnectException connExc)
        {
            System.err.println("could not connect");
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        
    }
    
}



It is printing only "hello" but I want a chat type application. I want that when client says anything the server should respond it.
Can anyone make suggestions about this.
Thanks in advance.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>