Ola pessoal, é so seguinte.
Tenho um trabalho da faculdade pra fazer e consiste no seguinte:
Criar uma aplicaçao java, que utilize paralelismo (Threds).
Então pensei o seguinte, criar dois códigos um para o cliente e outro para o servidor, e o servidor atende as requisições do cliente. blz.
abaixo seguirão dos códigos do servidor e cliente respectivamente:
import java.net.*;
import java.io.*;
public class TCPServer {
public static void main(String args[]){
try{
int serverPort = 7896;
ServerSocket listenSocket = new ServerSocket(serverPort);
while(true) {
Socket clientSocket = listenSocket.accept();
Connection c = new Connection(clientSocket); } }catch(IOException e)
{System.out.println("Listen:" + e.getMessage());} }
}
class Connection extends Thread {
DataInputStream in;
DataOutputStream out;
Socket clientSocket;
public Connection (Socket aClientSocket) {
try{
clientSocket = aClientSocket;
in = new DataInputStream(clientSocket.getInputStream());
out = new DataOutputStream(clientSocket.getOutputStream());
this.start(); }catch(IOException e){System.out.println("Connection:" + e.getMessage());} }
public void run(){
try{
//an echo server
String data = in.readUTF();
out.writeUTF(data);
clientSocket.close(); }catch(EOFException e){System.out.println("EOF:" + e.getMessage());
}catch(IOException e){System.out.println("IO:" + e.getMessage());} }
}
import java.net.*;
import java.io.*;
public class TCPClient {
public static void main(String args[]) {
Socket s = null;
try {
int serverPort = 4567;
s = new Socket(args[1], serverPort);
DataInputStream in = new DataInputStream(s.getInputStream());
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.writeUTF(args[0]);
String data = in.readUTF();
System.out.println("Received: " + data);
} catch(UnknownHostException e) {
System.out.println("Sock: " + e.getMessage());
} catch(EOFException e) {
System.out.println("EOF: " + e.getMessage());
} catch(IOException e) {
System.out.println("IO: " + e.getMessage());
} finally {
if(s != null)
try {
s.close();
} catch(IOException e) {
System.out.println("Close failed!");
}
}
}
}
Esses códigos eu peguei com meu professor.
Só que tem um problema.
Não sei coloca-los pra funcionar.
alguem pode me ajuar ai??
vlw.