socket programming in java

Solutions on MaxInterview for socket programming in java by the best coders in the world

showing results for - "socket programming in java"
Valery
29 Mar 2018
1// File Name GreetingServer.java
2import java.net.*;
3import java.io.*;
4
5public class GreetingServer extends Thread {
6   private ServerSocket serverSocket;
7   
8   public GreetingServer(int port) throws IOException {
9      serverSocket = new ServerSocket(port);
10      serverSocket.setSoTimeout(10000);
11   }
12
13   public void run() {
14      while(true) {
15         try {
16            System.out.println("Waiting for client on port " + 
17               serverSocket.getLocalPort() + "...");
18            Socket server = serverSocket.accept();
19            
20            System.out.println("Just connected to " + server.getRemoteSocketAddress());
21            DataInputStream in = new DataInputStream(server.getInputStream());
22            
23            System.out.println(in.readUTF());
24            DataOutputStream out = new DataOutputStream(server.getOutputStream());
25            out.writeUTF("Thank you for connecting to " + server.getLocalSocketAddress()
26               + "\nGoodbye!");
27            server.close();
28            
29         } catch (SocketTimeoutException s) {
30            System.out.println("Socket timed out!");
31            break;
32         } catch (IOException e) {
33            e.printStackTrace();
34            break;
35         }
36      }
37   }
38   
39   public static void main(String [] args) {
40      int port = Integer.parseInt(args[0]);
41      try {
42         Thread t = new GreetingServer(port);
43         t.start();
44      } catch (IOException e) {
45         e.printStackTrace();
46      }
47   }
48}
Kathryn
31 Jan 2019
1// File Name GreetingClient.java
2import java.net.*;
3import java.io.*;
4
5public class GreetingClient {
6
7   public static void main(String [] args) {
8      String serverName = args[0];
9      int port = Integer.parseInt(args[1]);
10      try {
11         System.out.println("Connecting to " + serverName + " on port " + port);
12         Socket client = new Socket(serverName, port);
13         
14         System.out.println("Just connected to " + client.getRemoteSocketAddress());
15         OutputStream outToServer = client.getOutputStream();
16         DataOutputStream out = new DataOutputStream(outToServer);
17         
18         out.writeUTF("Hello from " + client.getLocalSocketAddress());
19         InputStream inFromServer = client.getInputStream();
20         DataInputStream in = new DataInputStream(inFromServer);
21         
22         System.out.println("Server says " + in.readUTF());
23         client.close();
24      } catch (IOException e) {
25         e.printStackTrace();
26      }
27   }
28}
Naia
10 Feb 2019
1public class GreetServer {
2    private ServerSocket serverSocket;
3    private Socket clientSocket;
4    private PrintWriter out;
5    private BufferedReader in;
6
7    public void start(int port) {
8        serverSocket = new ServerSocket(port);
9        clientSocket = serverSocket.accept();
10        out = new PrintWriter(clientSocket.getOutputStream(), true);
11        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
12        String greeting = in.readLine();
13            if ("hello server".equals(greeting)) {
14                out.println("hello client");
15            }
16            else {
17                out.println("unrecognised greeting");
18            }
19    }
20
21    public void stop() {
22        in.close();
23        out.close();
24        clientSocket.close();
25        serverSocket.close();
26    }
27    public static void main(String[] args) {
28        GreetServer server=new GreetServer();
29        server.start(6666);
30    }
31}
Lorenzo
19 Aug 2016
1$ java GreetingServer 6066
2Waiting for client on port 6066...
3
Cassie
28 Mar 2017
1Socket Programming
queries leading to this page
socket in java full tutorialtcp java programjava server clientjava tcp client serversocket complete example javawhat is socket in javasocket programming in java 14java sokets tutorialsockets java tutorialhow hard is socket programming javawhat is a socket in javawhat is socket programming in javahow to create a socket in javadevelop java program for client and server setup where a client connects 2c sends messages to server and the server shows them using socket connection java tcp serversocket programming using tcp javasocket on 28 29 in javasocket n javageeks for geeks socket programming javaclient socket javatcp stream communication java codeport connection javajava client socket examplehow to code socket in javaserver client javajava sockets tutorialjava program socketdefine socket programming in javajava sockets programmonsocket 28 29 javajava socket interface2 way socket programming in javasocket programming java advancedclient server code in java using socketsocket syntax in javawhat are sockets javasocket programming javaexplain socket in java in detailsocket in java toturealjava server program 3fjava code for socket programhow to use java sockets to connect to a public servergeeks for geeks java socket programmingtcp chat application in javafind area in socket programming in javajava socket code examplejava socket programming exampleweb socket in javajava socket programming exampleswhat is a socket javasocket programming application in javajava socket socket object javasocket programming in java javatpointsimple applications with socket in javasocket implementation in javasocket ip adress javajava networking programmingnetwork programming in javaserver client with javaclient socket programming in javajava socket connectionjava socketsocket api in javajava using socketssocket client javajava socket tutorialclient server java applicationjava socket programming how does it worksocket programming in java progra programizdemo socket javawhat are sockets in javahow to create socket in javajava socket progrsamminghow to do socket programming in javasocket class in javajava sockets programming tutorialjava sockets programmingexplain socket in javasocket java definitionsocket javaclient socket in javawhat are java socketssocket programming javawhat is the socket class in java 3ftcp ip socket programming in javaexplain java sockets 3fjava socketscleint server javasocket meaning in java computer networkssocket programming in java java versionsocket programming in java programizjava socket programming javajava socket class use connection oriented programmingjava socket programming tutorialhow to do socket programming in java 3ftcp chat program in java java socket programmingjava client server programsocket server javahow to use sockets in javajava socket performanceclient server socket programming in javasocket programming in java algorithmsyntax to create socket in javajava socket programmingworking with java socketsjava socket programming with swingclient and server javasocket programmijng in javasocket programing javaworking of socket programming in javawhat is socket as used in javajava socket applicationsocket programming using javasocket programming in java established java socket connectionserver side socket programming in javasocket programming in java web application concepttcp algorithm javajava socket programming guidewhat is java socket programming 3fjava server socket tutorialjava socket examplesocket session java geeks for geekssocket programming in java client and serversockets with javaserver socket in javasocket in javasocket program in javawhat is needed for a socket to connect to a server applicationjava how use socketjava socket programbasic server client implentation javajava tutorial socket programmingjava socket 3d new socket 28 29what is socket programming in java oopserver client connection javagive an example of advanced socket programming in javasocket programming in java examplesocket code in javaclient server architecture in java codehow java sockets workmethods in socket programming in javasocket programming with javahow to connect server and client apps using javasockets javasocket programming java examplesocket java tutorialsocket programming in javawhat is sockets in java socket javasockets in javaimplement a socket using javasocket write javapurpose of socket javajava create sockettcp algorithm in javais java good for socket programmingjava socket classsocket programming java servertcp 2fip server side programming in javasocket in java programminghttps java socketsjava socket optionswhere do we do socket programming in javawhat is a java socketjava client server socket programtcp sockets in javahow to connect to my local socket in javajava client serverpacket programming in javaserver programming in javausing socket in javajava source code for socket programmingserver socket programming in javausing java socketssocket programming java swingsocket programming project using javaclient and server code for socket massage in javasocket java connectionsocket tutorial java client server network javatcp socket programming in javasocket connection javain java network programming class used for socketwhat are socket in javasecure socket programming javasocket programming in tcp using javajava server socket programming java socket communicationjava code for client server socket programmingsocket comuncation javasocket programing in javasocket implementation code in javasocket programming java example using swingjava server and clientsocket methods javasocket java examplejava tcp socketjava socket programming booksocket programming in java using tcp java server socket programming examplesuse of socket programming in javajavasocketssocket programming in java web applicationtcp client server program in javasocket programing server javahow to use socket in javasocket io in javatcp 2fip programming in javajava connect to a sockettcp socket programming javaclient server javarunning socket program in java terminalsocket session java geegs for geeksjava what is a socketjava socket programingsocket in javahow to get started in socket programming javadef socket in javajava tcp programmingsocket in java tutorialsocket programming in java