Sunteți pe pagina 1din 7

Facultatea de Cibernetica,Statistica si Informatica Economica,Bucuresti

PROIECT RETELE DE
CALCULATOARE

Trasca Roxana Mihaela


Grupa 1048
Seria Informatica A

Sa se construiasca o aplicatie Socket Client-Server folosind limbajul Java,pentru realizarea unui chat(trimitere de mesaje intre client si
server). Aplicatia va contine doua interfete una Client si una Server. Interfetele vor contine : un buton pentru startare server si unul pentru
conectare la server ,doua textArea unde vor fi afisare mesajele ,doua textField unde vor fi introduse mesajele si doua butoane de send.In
momentul in care se va da click pe butoanele de pornire server si conectare acesta se vor afisa doua mesaje: conectat la server si clientul a fost
acceptat.Se va construi si o clasa ReceiveMessage prin intermediul careia se realizeaza trimiterea de mesaje intre client si server.

SERVER CODE:
package proiect;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
*
* @author Roxxi
*/
public class ServerForm extends javax.swing.JFrame {
ServerSocket server = null;
Socket client = null;
DataOutputStream dos = null;
DataInputStream dis = null;

public ServerForm() {
initComponents();
}
private void btn_strtServerActionPerformed(java.awt.event.ActionEvent evt) {
try

{
server = new ServerSocket(2000); //conectare la portul 2000
client = server.accept();

//asteapta ca un client sa se conecteze

JOptionPane.showMessageDialog(null, "Client request accepted!"); //in momentul in care un client s-a conectat afisam
mesajul
dos = new DataOutputStream(client.getOutputStream());
dis = new DataInputStream(client.getInputStream());
ReceiveMessage serverThread = new ReceiveMessage(dis, txt_recMsg);
serverThread.setDaemon(true);
serverThread.setName("Client");
serverThread.start();
}
catch(IOException ex)
{

JOptionPane.showMessageDialog(null, "No client available!"); //in cazul in care nu gaseste niciun client disponibil

afisam mesajul
}
}
private void btn_sendActionPerformed(java.awt.event.ActionEvent evt) {
String msg = txt_msg.getText();
try{
dos.writeUTF(msg);//astepta un raspuns
}
catch (IOException ex) {
Logger.getLogger(ServerForm.class.getName()).log(Level.SEVERE, null, ex);
}
}

CLIENT CODE:
package proiect;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
*
* @author Roxxi
*/
public class ClientForm extends javax.swing.JFrame {
Socket server = null;
DataInputStream dis = null;
DataOutputStream dos = null;

public ClientForm() {
initComponents();
}
private void btn_connectActionPerformed(java.awt.event.ActionEvent evt) {

//creare buton pentru conectare la server

try {
//creare socket si asociere o adresa de server si un port
server = new Socket("127.0.0.1",2000);
JOptionPane.showMessageDialog(null, "Connected to server!"); //in momentul conectarii se afiseaza mesajul connected to
server
dis = new DataInputStream(server.getInputStream());
dos = new DataOutputStream(server.getOutputStream());
ReceiveMessage clientThread = new ReceiveMessage(dis, txt_recMsg);
clientThread.setDaemon(true);
clientThread.setName("Server");
clientThread.start();}
catch(UnknownHostException ex){

JOptionPane.showMessageDialog(null, "Connection failed!");} //in cazul in care conectarea esueaza afisam mesajul
connection failed
catch(IOException ex){
JOptionPane.showMessageDialog(null, "Connection failed!");}
}
private void btn_sendActionPerformed(java.awt.event.ActionEvent evt) {
try {
dos.writeUTF(txt_msg.getText());
}
catch (IOException ex) {
Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
} }

CLASA RECEIVEMESSAGE :

package proiect;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;

/**
*
* @author Roxxi
*/
public class ReceiveMessage extends Thread {
String msg = "";
DataInputStream dis = null;
JTextArea txt_area = null;

public ReceiveMessage(DataInputStream d,JTextArea a)


{
this.dis = d;
this.txt_area = a;
}
public void run()
{
while(true)
{
try {
msg = dis.readUTF(); //citeste mesajul codificat cu ajutorul UTf-8
txt_area.append("\n" + this.getName() + ":" + msg);
} catch (IOException ex) {
Logger.getLogger(ReceiveMessage.class.getName()).log(Level.SEVERE, null, ex);
}

}
}

}
REZULTATE:

TESTARE PORT:

TESTARE PROCES: