0% found this document useful (0 votes)
2 views3 pages

Java Socket Client and Server Example

The document contains Java code for a simple socket client-server application, where the client connects to the server to send and receive messages until 'End' is typed. It also includes examples of retrieving the InetAddress of the local host and named hosts, demonstrating how to obtain both a single and multiple addresses. The code showcases basic networking concepts using Java's socket programming capabilities.

Uploaded by

baturking1234
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Java Socket Client and Server Example

The document contains Java code for a simple socket client-server application, where the client connects to the server to send and receive messages until 'End' is typed. It also includes examples of retrieving the InetAddress of the local host and named hosts, demonstrating how to obtain both a single and multiple addresses. The code showcases basic networking concepts using Java's socket programming capabilities.

Uploaded by

baturking1234
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

*;
import [Link].*;

public class SocketClient {


private Socket socket;
private DataInputStream consoleInput; // from keyboard
private DataOutputStream out; // to server
private DataInputStream in; // from server

public SocketClient(String address, int port) {


try {
socket = new Socket(address, port);
[Link]("Connected to server");

consoleInput = new DataInputStream([Link]);


out = new DataOutputStream([Link]());
in = new DataInputStream([Link]());

String line = "";


while (![Link]("End")) {
// send message to server
[Link]("You: ");
line = [Link]();
[Link](line);

if ([Link]("End")) break;

// wait for server reply


String response = [Link]();
[Link]("Server: " + response);
}

// close resources
[Link]();
[Link]();
[Link]();
[Link]();

} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}

public static void main(String[] args) {


new SocketClient("[Link]", 5000);
}
}

import [Link].*;
import [Link].*;

public class SocketServer {


private Socket socket;
private ServerSocket server;
private DataInputStream in; // from client
private DataOutputStream out; // to client
private DataInputStream consoleIn; // from server keyboard
public SocketServer(int port) {
try {
server = new ServerSocket(port);
[Link]("Server started. Waiting for client...");

socket = [Link]();
[Link]("Client connected");

in = new DataInputStream([Link]());
out = new DataOutputStream([Link]());
consoleIn = new DataInputStream([Link]);

String line = "";


while (![Link]("End")) {
// wait for client message
line = [Link]();
[Link]("Client: " + line);

if ([Link]("End")) break;

// send reply to client


[Link]("Server: ");
String response = [Link]();
[Link](response);
}

// close resources
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();

} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}

public static void main(String[] args) {


new SocketServer(5000);
}
}

import [Link].*;

public class InetAddressExample1 {

public static void main(String[] args) throws UnknownHostException{

// To get and print InetAddress of the Local Host


InetAddress address = [Link]();

[Link]("InetAddress of the Local Host : "+address);

// To get and print host name of the Local Host


String hostName=[Link]();

[Link]("\nHost name of the Local Host : "+hostName);

import [Link].*;

public class InetAddressExample2 {

public static void main(String[] args)


throws UnknownHostException
{

// To get and print InetAddress of Named Hosts


InetAddress address1 = [Link](
"[Link]");

[Link]("Inet Address of named hosts : "


+ address1);

// To get and print ALL InetAddress of Named Host


InetAddress arr[] = [Link](
"[Link]");

[Link]("\nInet Address of ALL named hosts :");

for (int i = 0; i < [Link]; i++) {

[Link](arr[i]);
}
}
}

You might also like