Fazendo o trabalho de REC - Redes de computadores necessitei fazer um backup do que estou fazendo,
para conseguir implementá-lo;
Pois bem, segue o arquivo do cliente ("TCPClient .java") ;
----------------------------------------------------------------------------------------------
package rect;
import java.io.*;
import java.net.*;
class TCPClient {
private static BufferedReader inFromUser;
private static String sentence;
private static String modifiedSentence;
private static Socket clientSocket;
private static String host;
private static int porta;
private static DataOutputStream outToServer;
private static InputStreamReader inputStreamReader;
private static BufferedReader inFromServer ;
private static CharSequence strsair="tchau";
private static boolean sair=false;
public static void main(String argv[]) throws Exception
{
host="localhost";
porta=6789;
clientSocket = new Socket(host, porta);
for(int i=0; (i<4)&&(sair==false); i++)
{
inFromUser =new BufferedReader(
new InputStreamReader(System.in));
outToServer = new DataOutputStream( clientSocket.getOutputStream());
inputStreamReader=new InputStreamReader (clientSocket.getInputStream());
inFromServer = new BufferedReader (inputStreamReader);
/*sentença digitada pelo cliente*/
sentence = inFromUser.readLine();
if(sentence.contains(strsair)==true)
sair=true;
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
modifiedSentence="FROM SERVER: "+modifiedSentence+"\n sair ="+sair+"\n" ;
System.out.println(modifiedSentence);
}
System.out.println("desconectando servidor "+"clientSocket.toString()"+clientSocket.toString());
clientSocket.close();
}
public void testar(String argv[]) throws Exception
{
}
}
----------------------------------------------------------------------------------------------
Coloquei todas as variáveis como da classe para daqui a pouco implementar outros métodos
Não explicarei meu código.
Fiz no netbeans, usando projeto, devido a ser mais agradável,
o highlight da sintaxe e ele sugerir (recurso auto-completar) o código que você está digitando
O do servidor;
("TCPServer.java")
----------------------------------------------------------------------------------------------
package rect;
import java.io.*;
import java.net.*;
class TCPServer {
private static String clientSentence;
private static String capitalizedSentence;
private static ServerSocket welcomeSocket;
private static int porta;
private static Socket connectionSocket;
private static BufferedReader inFromClient;
private static InputStreamReader inputStreamReader;
private static InputStream getInputStream1;
private static OutputStream getOutputStream1;
private static DataOutputStream outToClient;
private static CharSequence strsair="tchau";
private static boolean sair=false;
public static void main(String argv[]) throws Exception
{
porta = 6789;
welcomeSocket = new ServerSocket(porta);
while(true) {
connectionSocket = welcomeSocket.accept();
for(int i=0; (i<4)&&(sair==false) ; i++)
{
getInputStream1=connectionSocket.getInputStream();
inputStreamReader= new InputStreamReader(getInputStream1);
inFromClient = new BufferedReader(inputStreamReader);
getOutputStream1 =connectionSocket.getOutputStream();
outToClient = new DataOutputStream(getOutputStream1 );
/* sentença do cliente cliente sentence*/
clientSentence = inFromClient.readLine();
if(clientSentence.contains(strsair)==true)
sair=true;
/*
* */
System.out.println("clientSentence = inFromClient.readLine(); ="+clientSentence+" sair= "+sair);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
/* mandando para o cliente outToClient.writeBytes(capitalizedSentence); */
outToClient.writeBytes(capitalizedSentence);
}
System.out.println("desconectando servidor "+"welcomeSocket.toString()"+welcomeSocket.toString());
}
}
}
----------------------------------------------------------------------------------------------
Assinar:
Postar comentários (Atom)
Nenhum comentário:
Postar um comentário