Commit f0781a31 authored by nargessalehi98's avatar nargessalehi98

Add client

parents
Pipeline #5121 canceled with stages
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
import java.net.*;
import java.io.*;
public class Client1 {
private Socket socket = null;
private DataInputStream console = null;
private DataOutputStream streamOut = null;
public Client1(String serverName, int serverPort) {
System.out.println("Establishing connection. Please wait ...");
try {
socket = new Socket(serverName, serverPort);
System.out.println("Connected: " + socket);
start();
} catch (UnknownHostException uhe) {
System.out.println("Host unknown: " + uhe.getMessage());
} catch (IOException ioe) {
System.out.println("Unexpected exception: " + ioe.getMessage());
}
String line = "";
while (!line.equals("over")) {
try {
assert console != null;
line = console.readLine();
streamOut.writeUTF(line);
streamOut.flush();
} catch (IOException ioe) {
System.out.println("Sending error: " + ioe.getMessage());
}
try {
DataInputStream streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String line2 = streamIn.readUTF();
System.out.println(line2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void start() throws IOException {
console = new DataInputStream(System.in);
streamOut = new DataOutputStream(socket.getOutputStream());
}
}
\ No newline at end of file
import java.io.IOException;
public class Main {
public static void main(String args[]){
Client1 client1=new Client1("Localhost",5001);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment