socket in java

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

showing results for - "socket in java"
Nolan
28 Aug 2019
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}
Lara
20 May 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}
Nicolò
26 Sep 2018
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}
Louanne
16 Mar 2018
1Socket Programming
María Camila
08 Nov 2020
1$ java GreetingServer 6066
2Waiting for client on port 6066...
3
queries leading to this page
what is socket programming in javasocket connection javajava source code for socket programminggeeks for geeks java socket programmingjava socket programming tutorialsocket programming in java web applicationsocket programing javadef socket in javasocket api in javajava socket programwhat is a socket in javajava socket java sockets programmingjava server and clientjava client server socket programtcp 2fip server side programming in javajava server program 3fsocket session java geegs for geeksserver side socket programming in javatcp sockets in javasocket meaning in java computer networkssocket n javatcp algorithm in javasocket programing in javatcp chat program in javapurpose of socket javajava sokets tutorialsocket java connectionclient server architecture in java codesocket syntax in javahow to connect to my local socket in javawhat is the socket class in java 3fsocket programming in java programiztcp java programjava how use socketdevelop 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 sockettcp socket programming in javasocket programmijng in javause of socket programming in javawhere do we do socket programming in javahow to code socket in javahow hard is socket programming javasocket javasocket 28 29 javajava client server programsocket programming using javasocket comuncation javasocket programming in java progra programizhow to do socket programming in javasocket implementation code in javaclient server socket programming in javasockets with javawhat is socket as used in javasimple applications with socket in javatcp ip socket programming in javapacket programming in javasocket programming using tcp javahow to get started in socket programming javasocket programming java advancedwhat is java socket programming 3fsecure socket programming javageeks for geeks socket programming javasocket tutorial javasocket programming javajava tutorial socket programmingjava socket programingsocket programming in java javatpointsocket java tutorialjava socket programming guideexplain socket in java in detailcleint server javasocket programming in java exampledemo socket javajava connect to a socketjava server socket tutorialjava socket connectionhow to use socket in javaserver client with javajava socket programmingsocket object javausing socket in javasocket java definitionjava socket programming javasocket program in javadefine socket programming in javasocket java examplesocket programming in java algorithm socket javabasic server client implentation javajava socket programming how does it worksocket in javasocket programming in java using tcp java tcp socketclient server javajava socket tutorialserver socket in javaclient and server javajava tcp client serverjava server clienttcp client server program in javawhat are java socketshttps java socketssocket programming in java java versionjava code for client server socket programmingjava networking programminghow to create socket in javasocket io in javajava socket examplejava socket interfacehow to use java sockets to connect to a public serversocket programming in java 14java socket programming exampleswhat is sockets in javawhat are sockets in javafind area in socket programming in javajava tcp serverjava socket classsocket in java full tutorialsocket programming application in javasocket programming in javajava client socket exampleclient socket programming in javawhat are socket in javajava socket progrsammingjava sockets programmonjava socket communicationjava socket programming booknetwork programming in javarunning socket program in java terminalhow java sockets workgive an example of advanced socket programming in javatcp chat application in javajava client serversocket programming project using javaserver client connection javahow to use sockets in javasocket programming java exampleis java good for socket programmingserver programming in javasocket programming with javajava socket programming examplesocket in java toturealjava sockets client server network javasocket code in javasocket programing server javamethods in socket programming in javajava create socketsocket programming in java client and serverhow to do socket programming in java 3fjava what is a socketjava sockets tutorialclient socket javaworking of socket programming in javatcp stream communication java codewhat is a java socketsocket write javasockets in javaexplain socket in javajava using sockets2 way socket programming in javatcp socket programming javaserver client javajava socket optionsjava socket performancewhat are sockets javaclient socket in javawhat is a socket javasocket ip adress javawhat is socket programming in java oopjava tcp programmingjava socket 3d new socket 28 29socket programming in tcp using javasocket on 28 29 in javasyntax to create socket in javajava sockets programming tutorialclient and server code for socket massage in javasocket in java programmingexplain java sockets 3fjava server socket programming socket implementation in javasocket server javaclient server java applicationsocket class in javasockets javajava server socket programming examplesjava socket applicationjava socket programming with swingsocket session java geeks for geekssocket programming in java web application conceptjava socket code examplewhat is socket in javasockets java tutorialwhat is needed for a socket to connect to a server applicationsocket programming java swingserver socket programming in javaworking with java socketsport connection javasocket programming java example using swingjava code for socket programweb socket in javahow to connect server and client apps using javatcp algorithm javasocket client javasocket in javasocket programming in java established java socket connectionsocket programming java serverin java network programming class used for socketsocket programming java java socket programmingjava program socketsocket methods javausing java socketssocket in java tutorialclient server code in java using sockettcp 2fip programming in javasocket complete example javajavasocketshow to create a socket in javajava socket class use connection oriented programmingimplement a socket using javasocket in java