This is a simple server/client program. What i'm trying to do is let the server send a message to the client, the client should receive this message and print this message on screen. When i run the program the server prints "Server start" like its supposed to but nothing happens on the client part ...Pls Help!

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

public class server1
{
    public static void main(String args[]) throws Exception
{
    try {
   DatagramSocket serverSocket = new DatagramSocket();

   InetAddress address = InetAddress.getByName("localhost");

  byte sendData[]=new byte[2048];
  byte receiveData[]=new byte[2048];

  System.out.println("Server Start");
  String Welcome = "You are now connected to server on this port  ";
  sendData = Welcome.getBytes();

  DatagramPacket packet = new DatagramPacket(sendData,sendData.length,address,7777);
  serverSocket.send(packet);
  serverSocket.close();
    }catch (Exception e) {
      System.err.println(e);
    }//end of catch

    }//end of main
    
}//end of class
import java.net.*;
import java.io.*;

public class client1 
{
     public static void main(String args[]) throws Exception
{
     try {
     DatagramSocket clientSocket = new DatagramSocket(7777);
     InetAddress address = InetAddress.getByName("localhost");
     
     byte[] sendData = new byte[2048];
     byte[] receiveData = new byte[2048];
          
     DatagramPacket packet = new DatagramPacket(receiveData,receiveData.length);
     while(true)
     {
     clientSocket.receive(packet);
     
     String sentence=new String(packet.getData());

     System.out.println("Server says: "+sentence+" ");
     clientSocket.close();
    }//end of while
    }catch (Exception e){
      System.err.println(e);
    }//end of catch
   } //end of main
}//end of client

hi Jeffery o
you have to used thread in server to keep continue running the server
so it can received connection request from client
i am giving this sample code from you

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

public class server1
{
 public static void main(String args[])
 {
  int i=1;
  System.out.println("Server started.waiting for connection........");
  try
  {
   ServerSocket serversocket =new ServerSocket(101);
   for(; ;)
   {
    Socket fromclient=serversocket.accept();
    System.out.println("client connected with id:"+i);
    Thread t=new ThreadedServer(fromclient,i);
    i++;
    t.start();
   }
  }
  catch(Exception e)
  {
    System.out.println("Error::" +e);
  }
 }
}


class ThreadedServer extends Thread
{
 Socket fromclient;
 int iCount;
 
 public ThreadedServer(Socket i,int c)
 {
  fromclient=i;
  iCount=c;
 }
 
 public void run()
 {
  try
  {
   int len;
   BufferedReader in=new BufferedReader(new InputStreamReader(fromclient.getInputStream()));
   PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(fromclient.getOutputStream())),true);
   String str1,str2,res;
   int ichoice;
   boolean done=true;
   do
   {
    ichoice=Integer.parseInt(in.readLine());
    switch(ichoice)
    {
     case 1:
            
            str1=in.readLine();
            System.out.println("the recieved 1st String is ::"+str1);          
            str2=in.readLine();
            System.out.println("the recieved 2nd String is ::"+str2); 
            res=str1+str2;
            out.println(res);
            break;
     case 2:
            str1=in.readLine();
            System.out.println("the recieved String is ::"+str1);          
            len=str1.length();
            out.println(len);
            break; 
            
     case 3:
            str1=in.readLine();
            System.out.println("the recieved String is ::"+str1);          
            res=str1.toUpperCase();
            out.println(res);
            break;            
     case 4:
            str1=in.readLine();
            System.out.println("the recieved  String is ::"+str1);          
            StringBuffer rev = new StringBuffer(str1);
            rev.reverse();
            out.println(rev);
            break;
     case 5:
            System.exit(0);
    }
   } while(ichoice<5);
   fromclient.close();
  }
 catch(Exception e)
 {
  System.out.println("Error:-"+e);
 }
}
}
import java.io.*;
import java.net.*;

public class client1
{
 public static final int port=101;
 public static void main(String args[]) throws Exception
 {
  int no1,no2,len;
  String str1,str2,res; 
  InetAddress addr=InetAddress.getLocalHost();
  Socket Toserver=new Socket(addr,port);
  System.out.println("client is connected:"+Toserver);
  BufferedReader streamfromserver=new BufferedReader(new InputStreamReader(Toserver.getInputStream()));
  BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
  PrintWriter streamtoserver=new PrintWriter(new BufferedWriter(new OutputStreamWriter(Toserver.getOutputStream())),true);
  int opt;
  do
  {
   System.out.println("\n\t\t------------------ MENU------------------ ");
   System.out.println("\n\t\t         1.Concatenation ");
   System.out.println("\n\t\t         2.Length ");
   System.out.println("\n\t\t         3.UpperCase");
   System.out.println("\n\t\t         4.Reverse");
   System.out.println("\n\t\t         5.exit");
   System.out.println("\n\t\t   Enter the option:");
   opt=Integer.parseInt(reader.readLine());
   String strVal=String .valueOf(opt);
   streamtoserver.println(strVal);
   switch(opt)
   {
    case 1: 
           System.out.println("\n enter the 1st String :-");
           str1=reader.readLine();
           streamtoserver.println(str1);
           System.out.println("\n enter the 2nd String :-");
           str2=reader.readLine();
           streamtoserver.println(str2);
           res=streamfromserver.readLine();
           System.out.println("\n the result is:-"+res);
           break;
    case 2:
           System.out.println("\n enter the String :-");
           str1=reader.readLine();
          
           streamtoserver.println(str1);
          
           
           len=Integer.parseInt(streamfromserver.readLine());
           System.out.println("\n the result is:-"+len);
           break; 
   case 3:
           System.out.println("\n enter the String :-");
           str1=reader.readLine();
         
           streamtoserver.println(str1);
           
           res=streamfromserver.readLine();
           System.out.println("\n the result is:-"+res);
           break; 
          
    case 4:
           System.out.println("\n enter the String :-");
           str1=reader.readLine();
          
           streamtoserver.println(str1);
          
           res=streamfromserver.readLine();
           System.out.println("\n the result is:-"+res);
           break;
    case 5:
           break; 
  }        
  } while(opt<5);
  System.out.println("\n closing client");
   Toserver.close();
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.