tyliang 0 Newbie Poster

I'm trying to send a message from client to server. Unfortunately, the server side crashes after my client send the message one time. Sorry for my bad english. I hope you all understand what I mean.

Client

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

public class Client {
    public static void main(String[] args) throws IOException
    {

        InetAddress address = InetAddress.getLocalHost();
        Socket myClient = new Socket(address, 9999);
        DataOutputStream output = new DataOutputStream(myClient.getOutputStream());

        output.writeBytes(args[0]);

        output.close();
        myClient.close();
    }

}

Server

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


public class Server
{
    public static void main(String[] args) throws IOException
    {
        //Create server socket
        ServerSocket myService = new ServerSocket(9999);

        while(true)
        {
            //client socket
            System.out.println("Waiting for connection.");
            Socket clientSocket = myService.accept();
            System.out.println("Accepted connection : " + clientSocket);

            //Write out received message
            BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            String line = input.readLine();
            System.out.println(line);

            //Close socket
            myService.close();
            clientSocket.close();
            input.close();         
        }
    }
}
Be a part of the DaniWeb community

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