Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network Programming with Java Sockets #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions 19-NetworkProgramming/Echo-TCP/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 19-NetworkProgramming/Echo-TCP/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions 19-NetworkProgramming/Echo-TCP/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 135 additions & 0 deletions 19-NetworkProgramming/Echo-TCP/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions 19-NetworkProgramming/Echo-TCP/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
public static void main(String[] args) {
try(Socket socket = new Socket("localhost",5000)){
BufferedReader iStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter oStream = new PrintWriter(socket.getOutputStream(),true);
Scanner scanner = new Scanner(System.in);

while(true){
System.out.print("input> ");
String message = scanner.nextLine();

oStream.println(message);
if(message.equalsIgnoreCase("exit")){
String response = iStream.readLine();
System.out.println(response);
break;
}
}

}catch (Exception e){
System.out.println("error: "+e.getMessage());
}
}
}
28 changes: 28 additions & 0 deletions 19-NetworkProgramming/Echo-TCP/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
public static void main(String[] args) {
try(ServerSocket serverSocket = new ServerSocket(5000)){
System.out.println("listening to port:5000");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preffer use logger

Socket clientSocket = serverSocket.accept();
System.out.println(clientSocket+" connected.");
BufferedReader iStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter oStream = new PrintWriter(clientSocket.getOutputStream(),true);
Comment on lines +11 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close stream
good practice is close stream by try()


while (true){
String message = iStream.readLine();
System.out.println("client: "+message);
if(message.equalsIgnoreCase("exit")){
oStream.println("connection closed.");
System.out.println("connection closed.");
break;
}
}

} catch (Exception e){
System.out.println("error: "+e.getMessage());
}
}
}
9 changes: 9 additions & 0 deletions 19-NetworkProgramming/Echo-UDP/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 19-NetworkProgramming/Echo-UDP/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions 19-NetworkProgramming/Echo-UDP/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 134 additions & 0 deletions 19-NetworkProgramming/Echo-UDP/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading