Skip to content

Commit

Permalink
Classes Updates - submit
Browse files Browse the repository at this point in the history
Updates nas classes ClientUPD e ServerUDP - socket e serversocket são instanciados, e esses recebem seus objetos Datagram.
  • Loading branch information
fsmpinheiro committed Jun 22, 2022
1 parent 99aea3d commit 6bdbc4a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 103 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="client"/>
<classpathentry exported="true" kind="lib" path="C:/Users/Samuel/Downloads/json-20220320.jar">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JSONLib"/>
<classpathentry kind="output" path="bin"/>
</classpath>
36 changes: 16 additions & 20 deletions client/client_Udp/ClientUDP.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,46 @@
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;


//ClientUDP recebe de Despachante e envia para UDPServer
public class ClientUDP{

DatagramSocket aSocket;
Socket cSocket;
DatagramSocket dataSocket;
DatagramPacket dataPacket;
InetAddress servip;
int serport;
String message;

public ClientUDP( String serverHost, int portNumber ){
try {
this.aSocket = new DatagramSocket();
this.servip = InetAddress.getByName( serverHost );
this.serport = portNumber;

} catch ( Exception e){
System.out.println("ClientUPD Exception: " + e.getMessage());
}
try {
this.cSocket = new Socket(serverHost, portNumber );
this.dataSocket = new DatagramSocket();

}catch ( Exception e) {
System.out.println("ClientUPD Exception: " + e.getMessage());
}
}



//sendRequest( ) envia uma mensagem em bytes
public void sendRequest( String strMessage ) {
try {
this.aSocket = new DatagramSocket();
this.message = strMessage;

byte [] mByte = message.getBytes();
this.dataPacket = new DatagramPacket( mByte, message.length(), this.servip, this.serport);
this.dataPacket = new DatagramPacket(mByte, message.length(), this.cSocket.getInetAddress(), this.cSocket.getPort() );

this.aSocket.send(dataPacket);
this.dataSocket.send( dataPacket );
this.dataPacket = null;
this.message = null;
} catch ( Exception e ) {

} catch ( SocketException e ) {
System.out.println("ClientUPD SocketException: " + e.getMessage());
} catch ( IOException e ) {
System.out.println("ClientUPD IOException: " + e.getMessage());
}


}
}

//getResponse( ) recebe uma mensagem em bytes[1024]
Expand All @@ -57,7 +53,7 @@ public String getResponse( ) {

byte [] buffer = new byte[1024];
this.dataPacket = new DatagramPacket( buffer, buffer.length );
this.aSocket.receive(this.dataPacket);
this.dataSocket.receive(this.dataPacket);
inResponse = new String( dataPacket.getData() );

} catch ( SocketException e ) {
Expand Down
103 changes: 20 additions & 83 deletions client/server_test/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

import javax.sound.midi.Receiver;

public class ServerTest{

DatagramSocket sSocket;
DatagramPacket dataPacket;

ServerSocket sSocket;
int serverPort = 7889;
boolean done;

Expand All @@ -23,32 +18,37 @@ public ServerTest() {

public void run( ) {
try {
this.sSocket = new DatagramSocket( serverPort );
byte [] buffer = new byte[1024];
this.sSocket = new ServerSocket ( serverPort );
DatagramSocket dataSocket = new DatagramSocket( sSocket.getLocalPort() );


while( !done ) {
this.dataPacket = new DatagramPacket( buffer, buffer.length );
sSocket.receive( dataPacket );
while( !this.done ) {
byte [] buffer = new byte[1024];
DatagramPacket dataPacket = new DatagramPacket( buffer, buffer.length );

dataSocket.receive( dataPacket );

String message = new String( dataPacket.getData() );
String message = new String ( dataPacket.getData() );
System.out.println("Mensagem do cliente: " + message );

message = null;

String str = "recebido";
byte [] respMessage = str.getBytes();

DatagramPacket response = new DatagramPacket( respMessage, respMessage.length, dataPacket.getAddress(), dataPacket.getPort() );
sSocket.send(response);
dataSocket.send( response );


}

} catch ( SocketException e ) {
System.out.println( "SocketException error: " + e.getMessage() );
} catch ( IOException e) {
System.out.println( "IOException error: " + e.getMessage() );
} finally {
if( sSocket != null ) {
this.done = true;
sSocket.close();
}
// } finally {
// if( sSocket.isClosed() ) {
// this.done = true;
// sSocket.close();
// }
}
}

Expand All @@ -59,66 +59,3 @@ public static void main( String[] args ) {
serv.run();
}
}


//private ServerSocket sSocket;
//private boolean done;
//
//public ServerTest( ){
// done = false;
//
//}
//
//
//public void run( ) {
// try {
// sSocket = new ServerSocket( 7998 );
// System.out.println("Servidor UPD iniciado, escutando na porta: " + sSocket.getLocalPort() + "...");
//
// while (!done) {
// Socket sClient = sSocket.accept();
// UDPSocket uSckt = new UDPSocket( sClient );
// uSckt.run();
//
// }
//
// } catch ( Exception e) {
// System.out.println("ServerTest.run error: " + e.getMessage());
// }
//}
//
//
//class UDPSocket implements Runnable{
// private Socket client;
// DatagramSocket sDatagram;
//
// public UDPSocket ( Socket client ) {
// try {
// this.client = client;
// this.sDatagram = new DatagramSocket();
// }catch( Exception e) {
// //TODO: error and exception on constructor;
// }
// }
//
// @Override
// public void run( ) {
// try {
// System.out.println("UDPSocket instanciado\n");
// //DatagramSocket sDatagram = new DatagramSocket();
// //sDatagram = new DatagramSocket();
// byte [ ] buffer = new byte[1024];
// DatagramPacket sDataPacket = new DatagramPacket(buffer, 1024);
//
// this.sDatagram.receive( sDataPacket );
//
// String received = new String( sDataPacket.getData(), 0 , sDataPacket.getLength() );
//
// this.sDatagram.close();
//
// System.out.println("Mensagem recebida: " + received );
// }catch (Exception e ) {
// System.out.println("UDPSocket.run error: " + e.getMessage() );
// }
// }
//}

0 comments on commit 6bdbc4a

Please sign in to comment.