Skip to content

Instantly share code, notes, and snippets.

@gusthavosouza
Created April 20, 2016 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusthavosouza/33eb89c4272d4e8bdc60bc5566dc6193 to your computer and use it in GitHub Desktop.
Save gusthavosouza/33eb89c4272d4e8bdc60bc5566dc6193 to your computer and use it in GitHub Desktop.
package uscs;
import java.io.*;
import java.net.*;
public class Atv02A {
@SuppressWarnings("deprecation")
public static void main(String args[]) {
String[] codigo = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10" };
String[] descr = { "Produto A", "Produto B", "Produto C", "Produto D", "Produto E", "Produto F", "Produto G",
"Produto H", "Produto I", "Produto G" };
try {
ServerSocket server2 = new ServerSocket(2222);
System.out.println("Servidor ativo na Porta 2222.");
Socket clientSocket = server2.accept();
DataInputStream is = new DataInputStream(clientSocket.getInputStream());
PrintStream os = new PrintStream(clientSocket.getOutputStream());
String line = is.readLine();
boolean ativo = true;
while (ativo) {
String retorna = "Produto nao encontrado";
if (line == null || line.trim().equals("99")) {
break;
}
for (int i = 0; i < 10; i++) {
if (line.trim().equals(codigo[i])) {
retorna = descr[i];
}
}
System.out.println("Recebido do cliente:" + line);
os.println(retorna);
line = is.readLine();
}
server2.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment