-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
56 lines (56 loc) · 1.87 KB
/
Copy pathServer.java
File metadata and controls
56 lines (56 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.io.*;
import java.net.*;
import java.util.Scanner;
class Server
{
public static void main(String args[])throws IOException
{
Scanner sc=new Scanner(System.in);
int port;
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDateTime now=LocalDateTime.now();
System.out.print("Enter the port to be used : ");
port=sc.nextInt();
ServerSocket s=new ServerSocket(port);
Socket s1=new Socket();
s1=s.accept();
DataInputStream dis=new DataInputStream(s1.getInputStream());
DataOutputStream dos=new DataOutputStream(s1.getOutputStream());
String message,mes;
System.out.print("Enter Your user name : ");
String us=sc.next();
dos.writeUTF(us);
mes=dis.readUTF();
File f=new File("D:\\"+us+" to "+mes+".txt");
FileWriter fw=new FileWriter(f,true);
PrintWriter pw=new PrintWriter(fw);
fw.write(dtf.format(now));
pw.println();
fw.close();
while(true)
{
dtf=DateTimeFormatter.ofPattern("HH:mm");
fw=new FileWriter(f,true);
pw=new PrintWriter(fw);
message=dis.readUTF();
now=LocalDateTime.now();
pw.println();
fw.write(dtf.format(now));
pw.println();
fw.write(mes+" : "+message);
pw.println();
pw.println();
System.out.println(mes+" : "+message);
System.out.print(us+" : ");
message=sc.nextLine();
fw.write(dtf.format(now));
pw.println();
fw.write(us+" : "+message);
pw.println();
dos.writeUTF(message);
fw.close();
}
};
};