Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews Coding/Programming Interviews:Active Template Library (ATL)ActiveXApplication DeveloperArtificial intelligenceAssemblyAssociate Software EngineerAWKAWTC ProgrammingC++ ProgrammingCGI PerlCGI ProgrammingCMMICobolCritical ReasoningData Structures TreesDCOM COMDelphiDTDE4XExtensible Stylesheet Language (XSL)FortranFull-Stack DeveloperHaskellHTML DOMILUIPhone DeveloperJasper Reports DeveloperJava DeveloperLisp ProgrammingLotus NotesMicrosoft Foundation Class (MFC)Mobile DeveloperMVC DeveloperNode.jsOOPPascalPerl ProgrammingPHPPHP DeveloperProgrammingProgramming AlgorithmsProgramming ConceptsPythonRubyRuby on RailsRuby on Rails DeveloperSenior Front End DeveloperSenior Software DeveloperSignature ProgramSOASocket ProgrammingSoftware Development EngineerSoftware engineeringSr. PHP ProgrammerStack And QueueSTLSwift DeveloperTCL (Tool Command Language)Team Leader Android DeveloperUMLUnity 2D Games DeveloperUnity 3D DeveloperUnity DeveloperVBA (Visual Basic for Applications)Visual Basic (VB)Visual C++Web DevelopmentWin32APIWindows ProgramingWordPress DevelopmentWSDLXFormsXHTMLXLinkXMLXPathXQueryXSL-FOXSLT
Copyright © 2018. All Rights Reserved
Socket Programming Interview Question:
Explain A multi-threaded DateServe that listens on port 3000 and waits for requests from clients?
Submitted by: AdministratorHere A multi-threaded DateServer that listens on port 3000 and waits for requests from clients. Whenever there is a request, the server replies by sending a Date object (over sockets) to the client as shown in Code Sample.
Code Sample: DateServer.java
import java.io.*;
import java.net.*;
import java.util.*;
public class DateServer
extends Thread {
private ServerSocket dateServer;
public static void main
(String argv[]) throws Exception {
new DateServer();
}
public DateServer() throws Exception {
dateServer = new ServerSocket(3000);
System.out.println
("Server listening on port 3000.");
this.start();
}
public void run() {
while(true) {
try {
System.out.println("Waiting for connections.");
Socket client = dateServer.accept();
System.out.println("Accepted a connection
from: "+ client.getInetAddress());
Connect c = new Connect(client);
} catch(Exception e) {}
}
}
}
class Connect extends Thread {
private Socket client = null;
private ObjectInputStream ois = null;
private ObjectOutputStream oos = null;
public Connect() {}
public Connect(Socket clientSocket) {
client = clientSocket;
try {
ois = new ObjectInputStream
(client.getInputStream());
oos = new ObjectOutputStream
(client.getOutputStream());
} catch(Exception e1) {
try {
client.close();
}catch(Exception e) {
System.out.println(e.getMessage());
}
return;
}
this.start();
}
public void run() {
try {
oos.writeObject(new Date());
oos.flush();
// close streams and connections
ois.close();
oos.close();
client.close();
} catch(Exception e) {}
}
}
Submitted by: Administrator
Code Sample: DateServer.java
import java.io.*;
import java.net.*;
import java.util.*;
public class DateServer
extends Thread {
private ServerSocket dateServer;
public static void main
(String argv[]) throws Exception {
new DateServer();
}
public DateServer() throws Exception {
dateServer = new ServerSocket(3000);
System.out.println
("Server listening on port 3000.");
this.start();
}
public void run() {
while(true) {
try {
System.out.println("Waiting for connections.");
Socket client = dateServer.accept();
System.out.println("Accepted a connection
from: "+ client.getInetAddress());
Connect c = new Connect(client);
} catch(Exception e) {}
}
}
}
class Connect extends Thread {
private Socket client = null;
private ObjectInputStream ois = null;
private ObjectOutputStream oos = null;
public Connect() {}
public Connect(Socket clientSocket) {
client = clientSocket;
try {
ois = new ObjectInputStream
(client.getInputStream());
oos = new ObjectOutputStream
(client.getOutputStream());
} catch(Exception e1) {
try {
client.close();
}catch(Exception e) {
System.out.println(e.getMessage());
}
return;
}
this.start();
}
public void run() {
try {
oos.writeObject(new Date());
oos.flush();
// close streams and connections
ois.close();
oos.close();
client.close();
} catch(Exception e) {}
}
}
Submitted by: Administrator
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.