Total members 11774 |It is currently Thu Apr 25, 2019 8:24 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






* Project Name:   Server Names Records
* Programmer:   msi_333 & karkar
* Type:   Networks and files
* Technology:  Java, sockets
* IDE:   NetBeans
* Description:   This program will maintain a set of records in a file. The user will be a client which will connect to the server to store or retrieve a name. The client must be a valid user with a name and password pair. The server will return the record if it exists, or store any new record. An administrator will have an ability to create and delete user accounts and to create, modify and delete records. In addition, the server will be able to disable and enable client connections. The clients will only be able to add or query records.



The client and the administrator will be two modules in the program. Each will have its own GUI. A user will log into the server with a user/password pair. An administrator will also log into the server with an administrator name/password pair. The server will NOT need a GUI, but you may create one if you wish. Each record will have a user SS#, first name last name, phone number. A client will open a socket to the server, log in, and request a record by ID or by first and last name. The server will retrieve the record and return it to the client. If the record does not exists, the server will respond accordingly, and the client will display that no such record exists. The client may also add a new record. This operation will always succeed, as long the SS# name is unique, and the user provides info for all fields.

The server will be multithreaded, allowing multiple clients connections. Be sure to synchronize methods which modify the file, such as storing new records. The server will run listeners on two ports: one for clients another for administrators.

code sample :
java code
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
*
* @author SAMI
*/
public class ServerConnector extends Thread {

private String Name;
private String password;
private int AdminPort = 9999;
private int UserPort = 9991;
private ByteBuffer ReadBuffer;
private ByteBuffer writeBuffer;
private SocketChannel SChan;
private Selector ReadSelector;
private CharsetDecoder asciiDecoder;
private JFrame loginFrame;

public void setLoginFrame(JFrame loginFrame) {
this.loginFrame = loginFrame;
}
// private String addr = "127.0.0.1";
private String hostname = "ahmed";

public ServerConnector(String Name, String password) {
this.Name = Name;
this.password = password;


ReadBuffer = ByteBuffer.allocateDirect(300);
writeBuffer = ByteBuffer.allocateDirect(300);
asciiDecoder = Charset.forName("US-ASCII").newDecoder();
}

public void ConnectAsAdmin() {
Connect(AdminPort,"a");
}

public void ConnectAsUser() {
Connect(UserPort,"u");
// System.out.println("N=" + Name + "&P=" + password+"&t="+type);
}

public void Connect(int PORT,String type) {
try {
ReadSelector = Selector.open();
InetAddress addr = InetAddress.getByName(hostname);
SChan = SocketChannel.open(new InetSocketAddress(addr, PORT));
SChan.configureBlocking(false);
SChan.register(ReadSelector, SelectionKey.OP_READ, new StringBuffer());
SendMassage("N=" + Name + "&P=" + password+"&T="+type);

} catch (Exception e) {
e.printStackTrace();
}
}



public void SendMassage(String messg) {
prepareBuffer(messg);
channelWrite(SChan);
System.out.println("message sent");
}

public void prepareBuffer(String massg) {
System.out.println("Massage=" + massg);
writeBuffer.clear();
writeBuffer.put(massg.getBytes());
writeBuffer.putChar('\n');
writeBuffer.flip();
}

public void channelWrite(SocketChannel client) {
long num = 0;
long len = writeBuffer.remaining();
while (num != len) {
try {
num += SChan.write(writeBuffer);

Thread.sleep(5);
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {

}

}

writeBuffer.rewind();
}

public String ReadMassage() {
String Massage = null;
try {

ReadSelector.selectNow();
Set readkeys = ReadSelector.selectedKeys();
Iterator iter = readkeys.iterator();
while (iter.hasNext()) {
SelectionKey key = (SelectionKey) iter.next();
iter.remove();

SocketChannel client = (SocketChannel) key.channel();
ReadBuffer.clear();

long num = client.read(ReadBuffer);

if (num == -1) {
return null;

} else {

StringBuffer str = (StringBuffer) key.attachment();
ReadBuffer.flip();
String data = asciiDecoder.decode(ReadBuffer).toString();
ReadBuffer.clear();
Massage = data;
System.out.println("Massage= " + Massage);
str.append(data);


return Massage;
}
}

} catch (IOException ex) {
ex.printStackTrace();
}

return Massage;
}
public void sendlogout()
{
SendMassage("logout&"+Name+"&"+password);
if(loginFrame==null)
System.out.println("The Frame is null");
else
loginFrame.setVisible(true);

}

public void Amout()
{
loginFrame.setVisible(true);
}
public void sendLogin(String type){
SendMassage("login:&"+Name+"&"+password+"&"+type);

}
public boolean changePassword(String oldPassword,String newPassword)
{
if(oldPassword.equals(password))
{
SendMassage("changePassword&"+Name+"&"+password+"&"+newPassword);
return true;
}
return false;

}

public void run() {

}
String getUserName()
{
return Name;
}

}

code sample 2:
java code
import java.io.BufferedReader;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author SAMI
*/
public class LoginIO {

private final String delim = "&";
private final String lineSeparator = (String) java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
BufferedReader buffRead;
BufferedWriter buffWrite;
String name;
String password;
File Accounts;
File Records;
FileWriter fr;
private String type;

public LoginIO() {

Accounts = new File("Account.txt");
Records = new File("records.txt");
// buffRead = new BufferedReader(new FileReader(Accounts));

//buffWrite = new BufferedWriter(new FileWriter(f,true));

// buffWrite = new BufferedWriter(new FileWriter(f));

// buffWrite=new BufferedWriter(new FileWriter(file));

}

public String checkUser(String Massage) {
ExtractNP(Massage);
String returns="This ID is not yet taken.";
try {
String Line = " ";
buffRead = new BufferedReader(new FileReader(Accounts));
while ((Line = buffRead.readLine()) != null) {
String[] data = Line.split("&");
System.out.println("len = " + data.length);
System.out.println("line " + Line);
System.out.println("Am here:" + data[1] + "-" + data[2]);
System.out.println("Am here:" + name + "-" + password);

if ((data[0].equals(name)) && (data[1].equals(password) && data[3].equalsIgnoreCase(type))) {
if(data[2].equalsIgnoreCase("c"))
{
returns= "User Aleardy connected esure that you disconnect or contact with admin";
System.out.println("returns "+returns);
break;
}
connectUser(name);
System.out.println("Is in file");
returns = "welcome "+name+" !";
}

// buffWrite.newLine();
}
buffRead.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return returns;
}


public String GetRecord(String SSID) {
// ExtractNP(Massage);
String Line = "no such record exists";
try {

buffRead = new BufferedReader(new FileReader("records.txt"));
while ((Line = buffRead.readLine()) != null) {
String[] data = Line.split("&");


if ((data[0].equals(SSID.trim()))) { //buffWrite.write("N="+name+"");
connectUser(name);
System.out.println("Is in file : " + Line);
return Line;
}

// buffWrite.newLine();
}
buffRead.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return "no such record exists";
}

public void ExtractNP(String Massage) {
name = Massage.substring(2, Massage.indexOf("&P"));
password = Massage.substring(Massage.indexOf("&P") + 3, Massage.indexOf("&T"));
type = Massage.substring(Massage.indexOf("&T") + 3, Massage.length() - 2);
System.out.println("Name=" + name + " Password=" + password+ " type=" + type);

}

synchronized boolean AddNewRecord(String SS, String fName, String lName, String phone) {

try {
buffWrite = new BufferedWriter(new FileWriter(Records, true));
String userData = SS + '&' + fName + '&' + lName + '&' + phone;


buffWrite.write(userData);
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();


} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
} finally {

}
return true;
}

synchronized String AddNewUser(String name,String password) {

String returnes = "problem cann't add";
try {
// ExtractNP(Message);
if(!canCreateAccount(name))
return name+" already exist";
buffWrite = new BufferedWriter(new FileWriter(Accounts, true));
buffWrite.write(name + '&' + password + "&N&u");
buffWrite.newLine();
//System.out.println(canCreateRecord("SN"));
buffWrite.flush();
buffWrite.close();
returnes="Account added ";
// AddNewRecord(getNewSSNo(), name, name, "1234");

} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//makeUserAdmin();
}
return returnes;
}
public boolean canCreateAccount(String SS) {
try {
buffRead.close();
buffRead = new BufferedReader(new FileReader(Accounts));
String Line = " ";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(SS)) {
return false;

}

// buffWrite.newLine();


}

} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
public boolean canCreateRecord(String SS) {
try {
buffRead.close();
buffRead = new BufferedReader(new FileReader(Records));
String Line = " ";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(SS)) {
return false;

}

// buffWrite.newLine();


}

} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}

public String getAllOnlineUsers() {
String OnlineUsers = "";


try {
buffRead.close();
buffRead = new BufferedReader(new FileReader(Accounts));
String Line = " ";
System.out.println("Just here");
while ((Line = buffRead.readLine()) != null) {

String[] tokens = Line.split("&");
if (tokens == null) {
continue;
}
System.out.println("LEN = " + tokens.length);
if (tokens[2].equalsIgnoreCase("C")) {
OnlineUsers += tokens[0] + "&";
System.out.println("OnlineUsers= " + tokens[0]);

}

// buffWrite.newLine();
}
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("OnlineUsers= " + OnlineUsers);
return OnlineUsers;
}
/*synchronized public void makeUserAdmin(){
try {
buffRead = new BufferedReader(new FileReader(Accounts));
String fileContent="";
String Line = " ";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");
System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[4].equalsIgnoreCase("u")) {
fileContent+=tokens[0]+delim+tokens[1]+delim+tokens[2]+delim+tokens[3]+delim+"a"+lineSeparator;
}else
fileContent+=Line+lineSeparator;
// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter(Accounts));
buffWrite.write(fileContent);
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
}*/

synchronized String ModifyRecord(String SSL, String Record) {
System.out.println("SSL=" + SSL + " Record=" + Record);
String returns = "Record is not found";
try {
buffRead = new BufferedReader(new FileReader("records.txt"));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");

/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(SSL)) {
fileContent += Record + lineSeparator;
returns = "Record is modified";

} else {
fileContent += Line + lineSeparator;
}

// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter("records.txt"));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();

} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);

}
return returns;
}

synchronized String DeleteRecord(String SSL) {
System.out.println("SSL=" + SSL);
String returns = "Record is not found";
try {
buffRead = new BufferedReader(new FileReader("records.txt"));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");

/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(SSL)) {
// fileContent+=Record+lineSeparator;
returns = "Record is deleted";

} else {
fileContent += Line + lineSeparator;
}

// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter("records.txt"));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();

} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);

}
return returns;
}

synchronized public void DisconnectUser(String Username) {
try {
buffRead = new BufferedReader(new FileReader(Accounts));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");

/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(Username.trim()) && tokens[2].equalsIgnoreCase("C")) {
fileContent += tokens[0] + delim + tokens[1] + delim + "N" + delim + tokens[3] + lineSeparator;
} else {
fileContent += Line + lineSeparator;
}

// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter(Accounts));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();

} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);

}
}

synchronized public void connectUser(String Username) {
System.out.println("Connecting User : " + Username);
try {
buffRead = new BufferedReader(new FileReader(Accounts));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");

/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(Username.trim())) {

fileContent += tokens[0] + delim + tokens[1] + delim + "C" + delim + tokens[3] + lineSeparator;
System.out.println(tokens[0] + delim + tokens[1] + delim + "C" + delim + tokens[3] + lineSeparator);

} else {
fileContent += Line + lineSeparator;
}

// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter(Accounts));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();

} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);

}
}

public String getNewSSNo() {
return new String("S" + Math.round(Math.random() * 100));
}

synchronized public void changePassword(String name, String oldPass, String newPass) {
try {
buffRead = new BufferedReader(new FileReader(Accounts));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");

/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(name.trim()) && tokens[1].equals(oldPass.trim())) {

fileContent += tokens[0] + delim + newPass + delim + tokens[2] + delim + tokens[3] + lineSeparator;
System.out.println("Password "+tokens[0] + delim + tokens[1] + delim + "C" + delim + tokens[3] + lineSeparator);

} else {
fileContent += Line + lineSeparator;
}

// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter(Accounts));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();

} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);


}
}
public String GetRecord(String fName,String lName) {
// ExtractNP(Massage);
String Line = "no such record exists";
try {

buffRead = new BufferedReader(new FileReader("records.txt"));
while ((Line = buffRead.readLine()) != null) {
String[] data = Line.split("&");


if ((data[1].equalsIgnoreCase(fName.trim())&&data[2].equalsIgnoreCase(lName.trim()))) { //buffWrite.write("N="+name+"");
// connectUser(name);
System.out.println("Is in file : " + Line);
return Line;
}

// buffWrite.newLine();
}
buffRead.close();
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);
}
return "no such record exists";
}
synchronized String DeleteAccount(String fname) {
// System.out.println("SSL=" + SSL);
String returns = "no such account found";
try {
buffRead = new BufferedReader(new FileReader(Accounts));
String fileContent = "";
String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");

/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(fname)) {
// fileContent+=Record+lineSeparator;
returns = fname+"Account is deleted";

} else {
fileContent += Line + lineSeparator;
}

// buffWrite.newLine();
}
buffRead.close();
buffWrite = new BufferedWriter(new FileWriter(Accounts));
buffWrite.write(fileContent.trim());
buffWrite.newLine();
buffWrite.flush();
buffWrite.close();

} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);

}
return returns;
}
synchronized String Login(String fname,String Password,String type) {

String returns = "This ID is not yet taken.";
try {
// System.out.println("SSL=" + SSL);


buffRead = new BufferedReader(new FileReader(Accounts));

String Line = "";
while ((Line = buffRead.readLine()) != null) {
String[] tokens = Line.split("&");

/// System.out.println("OnlineUsers= " + tokens[0]);
if (tokens[0].equalsIgnoreCase(fname.trim())&&tokens[1].equals(Password.trim())&&
tokens[3].equalsIgnoreCase(type.trim())) {
// fileContent+=Record+lineSeparator;
returns = "Welcome "+fname+" !" ;
System.out.println(returns);
connectUser(fname);
return returns;

}
buffRead.close();



return returns;
}
} catch (IOException ex) {
Logger.getLogger(LoginIO.class.getName()).log(Level.SEVERE, null, ex);

}
return returns;
}
}


Attachment:
page1.gif
page1.gif [ 9.15 KiB | Viewed 4789 times ]

Attachment:
page2.gif
page2.gif [ 14.78 KiB | Viewed 3853 times ]





Attachments:
File comment: NetBeans projects (Server + client )
5A.zip [111.22 KiB]
Downloaded 607 times
Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time

updated.



Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Server Names Records
 print all files names in folder     -  
 Get MX records corresponding to a given Internet host name     -  
 SQL Limit query for a range of records     -  
 Search records from text file     -  
 Sql server or windows server question?     -  
 FTP Server and FTP Client     -  
 FTP Server Softwares     -  
 proxy server     -  
 Get Server address     -  
 Ping server     -  



Topic Tags

Java Projects, Java Networking







Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com