forked from AdvancedProgramming1404/HW-10-Socket-Programming
implement Chat message
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
package com.university.chat.Client;
|
package com.university.chat.Client;
|
||||||
|
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
|
||||||
public class ServerListener implements Runnable{
|
public class ServerListener implements Runnable{
|
||||||
|
public ServerListener(ObjectInputStream in) {
|
||||||
|
}
|
||||||
// TODO: store the ObjectInputStream from the user socket
|
// TODO: store the ObjectInputStream from the user socket
|
||||||
// (this should be the same input stream the
|
// (this should be the same input stream the
|
||||||
// chatClient created when connecting)
|
// chatClient created when connecting)
|
||||||
|
|||||||
@@ -1,7 +1,33 @@
|
|||||||
package com.university.chat.Client;
|
package com.university.chat.Client;
|
||||||
|
|
||||||
|
import com.university.chat.Common.ChatMessage;
|
||||||
|
import com.university.chat.Common.FileMessage;
|
||||||
|
import com.university.chat.Common.MessageType;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class chatClient {
|
public class chatClient {
|
||||||
public static void main() {
|
public static void main() throws IOException {
|
||||||
|
Socket socket = new Socket("localhost" , 8080) ;
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()) ;
|
||||||
|
ObjectInputStream in = new ObjectInputStream(socket.getInputStream()) ;
|
||||||
|
Scanner scanner = new Scanner(System.in) ;
|
||||||
|
System.out.println("Enter Your UserName");
|
||||||
|
String username = scanner.nextLine() ;
|
||||||
|
ChatMessage logmsg = new ChatMessage(MessageType.LOGIN , username , null , null) ;
|
||||||
|
out.writeObject(logmsg);
|
||||||
|
out.flush();
|
||||||
|
ServerListener serverListener = new ServerListener(in) ;
|
||||||
|
Thread thread = new Thread(serverListener) ;
|
||||||
|
thread.start();
|
||||||
|
|
||||||
// TODO: Connecting to the server
|
// TODO: Connecting to the server
|
||||||
// 1. Create a socket and connect to the server
|
// 1. Create a socket and connect to the server
|
||||||
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in)
|
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in)
|
||||||
@@ -12,6 +38,46 @@ public class chatClient {
|
|||||||
|
|
||||||
while (true){
|
while (true){
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
String message = scanner.nextLine() ;
|
||||||
|
if (message.startsWith("/msg"))
|
||||||
|
{
|
||||||
|
String[] part = message.split(" " , 3) ;
|
||||||
|
if (part.length >=3)
|
||||||
|
{
|
||||||
|
String receiver = part[1];
|
||||||
|
String messageText = part[2];
|
||||||
|
ChatMessage privetMsg = new ChatMessage(MessageType.PRIVATE_MESSAGE , username , receiver , messageText) ;
|
||||||
|
out.writeObject(privetMsg);
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
}
|
||||||
|
} else if (message.startsWith("/User")) {
|
||||||
|
ChatMessage userRequest = new ChatMessage(MessageType.USER_LIST , username , null , null) ;
|
||||||
|
out.writeObject(userRequest);
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
} else if (message.startsWith("/sendfile")) {
|
||||||
|
String[] part = message.split(" " , 3) ;
|
||||||
|
if (part.length>=3)
|
||||||
|
{
|
||||||
|
String receiver = part[1] ;
|
||||||
|
String path = part[2] ;
|
||||||
|
File file = new File(path) ;
|
||||||
|
if (file.exists() && file.isFile())
|
||||||
|
{
|
||||||
|
byte[] fileBytes = Files.readAllBytes(Paths.get(path)) ;
|
||||||
|
FileMessage fileMsg = new FileMessage(username ,receiver ,file.getName() , fileBytes);
|
||||||
|
out.writeObject(fileMsg);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("file does not exist !");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Program loop — read a line from the console and act on it:
|
// TODO: Program loop — read a line from the console and act on it:
|
||||||
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
|
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
|
||||||
// - "/users" -> build & send a USER_LIST request
|
// - "/users" -> build & send a USER_LIST request
|
||||||
|
|||||||
Reference in New Issue
Block a user