How To Send Message To Server Using Socket In Java

Here is simple class to send message to server. You can change the IP and port server based on your requirement. I use and port 8080 for example. The message that you want to send will be from input dialog.

import javax.swing.JOptionPane;

public class SimpleClient {
    public static void main(String[]args){
        String msg = JOptionPane.showInputDialog("Please input message "); //get msg
        if(msg != null && !msg.isEmpty()){ //if not empty
            try {
                Socket socket = new Socket("", 8080); //create socket
                OutputStream outStream = socket.getOutputStream(); //get the outputstream of socket
                outStream.write(msg.getBytes()); //send msg
            } catch (IOException e) {

Hope this simple sample can help.. 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s