Skip to content

Instantly share code, notes, and snippets.

@behitek
Created February 27, 2017 13:47
Show Gist options
  • Save behitek/5edba4384c20e8163fa3cd8ab477a585 to your computer and use it in GitHub Desktop.
Save behitek/5edba4384c20e8163fa3cd8ab477a585 to your computer and use it in GitHub Desktop.
Java Client Server Demo using Socket
package com.hieunv.ltht;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Scanner;
import static java.net.InetAddress.getByName;
/**
* Created by HieuNguyen on 02/25/2017.
*/
public class ClientSide {
public static void main(String[] args) {
// Địa chỉ máy chủ.
String serverHost = "localhost";
int port = 80;
Socket socketOfClient = null;
BufferedWriter os = null;
BufferedReader is = null;
Scanner sc = new Scanner(System.in);
try {
try {
System.out.println("Enter serverHost: ");
serverHost = sc.nextLine();
System.out.println("Enter port: ");
port = sc.nextInt();
socketOfClient = new Socket(serverHost, port);
} catch (IOException e) {
System.out.println("Error when connect to serverHost, try again");
}
os = new BufferedWriter(new OutputStreamWriter(socketOfClient.getOutputStream()));
is = new BufferedReader(new InputStreamReader(socketOfClient.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + serverHost);
return;
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " + serverHost);
return;
}
System.out.println("Connect to "+serverHost+":"+port+" success!");
try {
while (true) {
System.out.println("\nMENU");
System.out.println("1.Giai PTB2");
System.out.println("2.Giai PT bac nhat hai an");
System.out.println("3.Exit");
System.out.println("Your choice: ");
int ch;
do{
ch = sc.nextInt();
if(ch < 1 || ch > 3){
System.out.println("Try again: ");
}
}while (ch < 1 || ch > 3);
if(ch == 1){
System.out.println("Enter value a,b,c follow syntax \"a b c\" : ");
sc.nextLine();
String s =sc.nextLine();
os.write("1 "+s);
os.newLine();
os.flush();
}else if(ch == 2){
System.out.println("Enter value a1,b1,c1,a2,b2,c2 follow syntax \"a1 b1 c1 a2 b2 c2\" : ");
sc.nextLine();
String s =sc.nextLine();
os.write("2 "+s);
os.newLine();
os.flush();
}else{
os.write("EXIT");
os.flush();
System.out.println("Connect is lost!");
break;
}
System.out.println(is.readLine());
}
os.close();
is.close();
socketOfClient.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
package com.hieunv.ltht;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
* Created by HieuNguyen on 02/25/2017.
*/
public class ServerSide {
public static void main(String args[]) throws IOException {
ServerSocket listener = null;
int clientNumber = 0;
try {
listener = new ServerSocket(7777);
} catch (IOException e) {
System.out.println(e);
System.exit(0);
}
System.out.println("Server is waiting to accept user...");
try {
while (true) {
Socket socketOfServer = listener.accept();
new ServiceThread(socketOfServer, clientNumber++).start();
}
} finally {
listener.close();
}
}
private static void log(String message) {
System.out.println(message);
}
private static class ServiceThread extends Thread {
private int clientNumber;
private Socket socketOfServer;
public ServiceThread(Socket socketOfServer, int clientNumber) {
this.clientNumber = clientNumber;
this.socketOfServer = socketOfServer;
log("New connection with client# " + this.clientNumber + " at " + socketOfServer);
}
private String giaiPTB1(double a1,double b1,double c1,double a2,double b2, double c2){
String rs = "";
double D,Dx,Dy;
D=a1*b2-a2*b1; Dx=c1*b2-c2*b1; Dy=a1*c2-a2*c1;
if(D == 0){
if(Dx == 0 && Dy == 0){
rs = "PT bac nhat co vo so nghiem!";
}else{
rs = "PT bac nhat vo nghiem!";
}
}else{
rs = "PT bac nhat co nghiem duy nhat (x,y) = (" + (Dx/D)+","+(Dy/D)+")";
}
return rs;
}
private String giaiPTB2(double a, double b, double c){
String result = "";
if(a == 0){
if(b == 0){
if(c == 0){
result += "Phuong trinh bac nhat(a = 0) co vo so nghiem!";
}else{
result += "Phuong trinh bac nhat(a = 0) vo nghiem!";
}
}else{
result += "Phuong trinh bac nhat(a = 0) co nghiem: x = " + (-c/b);
}
}else{
double delta = b*b - a*c*4;
if(delta < 0){
result += "Phuong trinh bac hai vo nhgiem!";
}else if(delta == 0){
result += "Phuong trinh bac hai co ngiem kep x1 = x2 = " + (-b/(2*a));
}else{
result += "Phuong trinh bac hai co 2 nghiem phan biet: x1 = " + (-b + Math.sqrt(delta))/(2*a) + ", x2 = " + (-b - Math.sqrt(delta))/(2*a);
}
}
return result;
}
@Override
public void run() {
try {
BufferedReader is = new BufferedReader(new InputStreamReader(socketOfServer.getInputStream()));
BufferedWriter os = new BufferedWriter(new OutputStreamWriter(socketOfServer.getOutputStream()));
while (true) {
String line = is.readLine();
if(line == null) continue;
if(line.equals("EXIT")){
log("Lost connection of client# " + this.clientNumber + " at " + socketOfServer);
log("Server still listening client request...");
}else{
String s[] = line.trim().split(" ");
int ch = 0;
double a,b,c;
try{
ch = Integer.parseInt(s[0]);
a = Double.parseDouble(s[1]);
b = Double.parseDouble(s[2]);
c = Double.parseDouble(s[3]);
String result = "Server: ";
if(ch == 1){
result += giaiPTB2(a,b,c);
}else{
double a1 = Double.parseDouble(s[4]);
double b1 = Double.parseDouble(s[5]);
double c1 = Double.parseDouble(s[6]);
result += giaiPTB1(a,b,c,a1,b1,c1);
}
os.write(result);
os.newLine();
os.flush();
}catch (Exception e){
os.write("Server: Input is error!");
os.newLine();
os.flush();
}
}
}
} catch (IOException e) {
System.out.println(e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment