// ********************************************************************************** // // Theoretically, you shouldn't have to alter anything below this point in this file // unless you want to change the color of your agent // // ********************************************************************************** public void getConnected(String args[]) { try { // initial connection int port = 3000 + Integer.parseInt(args[1]); s = new Socket(args[0], port); sout = new PrintWriter(s.getOutputStream(), true); sin = new BufferedReader(new InputStreamReader(s.getInputStream())); // read in the map of the world numNodes = Integer.parseInt(sin.readLine()); int i, j; for (i = 0; i < numNodes; i++) { world[i] = new node(); String[] buf = sin.readLine().split(" "); world[i].posx = Double.valueOf(buf[0]); world[i].posy = Double.valueOf(buf[1]); world[i].numLinks = Integer.parseInt(buf[2]); // System.out.println(world[i].posx + ", " + world[i].posy); for (j = 0; j < 4; j++) { if (j < world[i].numLinks) { world[i].links[j] = Integer.parseInt(buf[3 + j]); // System.out.println("Linked to: " + world[i].links[j]); } else world[i].links[j] = -1; } } currentNode = Integer.parseInt(sin.readLine()); String myinfo = args[2] + "\n" + "0.7 0.45 0.45\n"; // name + rgb values; i think this is color is pink // send the agents name and color sout.println(myinfo); } catch (IOException e) { System.out.println(e); } }
/** Start talking to the server */ public void start() { String codehost = getCodeBase().getHost(); if (socket == null) { try { // Open the socket to the server socket = new Socket(codehost, port); // Create output stream out = new ObjectOutputStream(socket.getOutputStream()); out.flush(); // Create input stream and start background // thread to read data from the server in = new ObjectInputStream(socket.getInputStream()); new Thread(this).start(); } catch (Exception e) { // Exceptions here are unexpected, but we can't // really do anything (so just write it to stdout // in case someone cares and then ignore it) System.out.println("Exception! " + e.toString()); e.printStackTrace(); setErrorStatus(STATUS_NOCONNECT); socket = null; } } else { // Already started } if (socket != null) { // Make sure the right buttons are enabled start_button.setEnabled(false); stop_button.setEnabled(true); setStatus(STATUS_ACTIVE); } }
public Cliente(Socket s) { super("OPERACIONES CON BD"); socket = s; try { salida = new DataOutputStream(socket.getOutputStream()); inObjeto = new ObjectInputStream(socket.getInputStream()); } catch (IOException e) { e.printStackTrace(); System.exit(0); } setLayout(null); etiqueta.setBounds(10, 10, 200, 30); add(etiqueta); empconsultar.setBounds(210, 10, 50, 30); add(empconsultar); textarea1 = new JTextArea(); scrollpane1 = new JScrollPane(textarea1); scrollpane1.setBounds(10, 50, 400, 300); add(scrollpane1); boton.setBounds(420, 10, 100, 30); add(boton); desconectar.setBounds(420, 50, 100, 30); add(desconectar); textarea1.setEditable(false); boton.addActionListener(this); desconectar.addActionListener(this); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); }
public void startServer() throws Exception { while (true) { Socket s = server.accept(); cClient.add(new ClientConn(s)); ta.append("NEW-CLIENT " + s.getInetAddress() + ":" + s.getPort()); ta.append("\n" + "CLIENTS-COUNT: " + cClient.size() + "\n\n"); } }
public void setUpNetworking() { try { Socket sock = new Socket("192.168.0.117", 5555); PrintWriter writer = new PrintWriter(sock.getOutputStream()); System.out.println("networking established"); } catch (IOException ex) { ex.printStackTrace(); } }
public ServerAgentThread(Server father, Socket sc) { this.father = father; this.sc = sc; try { din = new DataInputStream(sc.getInputStream()); dout = new DataOutputStream(sc.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } }
public Client(int port) { Socket socket = null; try { socket = new Socket("127.0.0.1", port); chatField.setText("------CONNECTION ESTABLISHED------"); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); enableChat(); } catch (Exception e) { chatField.setText("Connection Failed"); } }
// 서버와 연결 public void connection() { try { s = new Socket("localhost", 65535); // s=>server in = new BufferedReader(new InputStreamReader(s.getInputStream())); // 서버로 값을 읽어들임 out = s.getOutputStream(); // 서버로 값을 보냄 /*out.write((Function.LOGIN+"|"+id+"|" +pass+"\n").getBytes());*/ } catch (Exception ex) { } new Thread(this).start(); // run()으로 이동 // 서버로부터 응답값을 받아서 처리 }
public Server(int port) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); clientSocket = serverSocket.accept(); chatField.setText("------CONNECTION ESTABLISHED------"); out = new PrintWriter(clientSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); enableChat(); } catch (IOException e) { chatField.setText("Connection Fail"); } }
/** * ** Reads a line from the specified socket's input stream ** @param socket The socket to read a * line from ** @param maxLen The maximum length of of the line to read ** @param sb The string * buffer to use ** @throws IOException if an error occurs or the server has stopped */ protected static String socketReadLine(Socket socket, int maxLen, StringBuffer sb) throws IOException { if (socket != null) { int dataLen = 0; StringBuffer data = (sb != null) ? sb : new StringBuffer(); InputStream input = socket.getInputStream(); while ((maxLen < 0) || (maxLen > dataLen)) { int ch = input.read(); // Print.logInfo("ReadLine char: " + ch); if (ch < 0) { // this means that the server has stopped throw new IOException("End of input"); } else if (ch == LineTerminatorChar) { // include line terminator in String data.append((char) ch); dataLen++; break; } else { // append character data.append((char) ch); dataLen++; } } return data.toString(); } else { return null; } }
// Cleanup for disconnect private static void cleanUp() { try { if (hostServer != null) { hostServer.close(); hostServer = null; } } catch (IOException e) { hostServer = null; } try { if (socket != null) { socket.close(); socket = null; } } catch (IOException e) { socket = null; } try { if (in != null) { in.close(); in = null; } } catch (IOException e) { in = null; } if (out != null) { out.close(); out = null; } }
// メッセージ監視用のスレッド public void run() { try { InputStream input = socket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); while (!socket.isClosed()) { String line = reader.readLine(); String[] msg = line.split(" ", 2); String msgName = msg[0]; String msgValue = (msg.length < 2 ? "" : msg[1]); reachedMessage(msgName, msgValue); } } catch (Exception err) { } }
/** Stop talking to the server */ public void stop() { if (socket != null) { // Close all the streams and socket if (out != null) { try { out.close(); } catch (IOException ioe) { } out = null; } if (in != null) { try { in.close(); } catch (IOException ioe) { } in = null; } if (socket != null) { try { socket.close(); } catch (IOException ioe) { } socket = null; } } else { // Already stopped } // Make sure the right buttons are enabled start_button.setEnabled(true); stop_button.setEnabled(false); setStatus(STATUS_STOPPED); }
/** * ** Writes the specified byte array to the specified socket's output stream ** @param socket The * socket which's output stream to write to ** @param b The byte array to write to the socket * output stream ** @throws IOException if an error occurs */ protected static void socketWriteBytes(Socket socket, byte b[]) throws IOException { if ((socket != null) && (b != null)) { OutputStream output = socket.getOutputStream(); output.write(b); output.flush(); } }
public void disconnect() { try { dos.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } }
public void disconnect() { try { dos.close(); s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void dispose() { try { if (s != null) s.close(); cClient.remove(this); ta.append("A client out! \n"); ta.append("CLIENT-COUNT: " + cClient.size() + "\n\n"); } catch (Exception e) { e.printStackTrace(); } }
/** * ** Writes <code>length</code> bytes from the specified byte array ** starting at <code>offset * </code> to the specified socket's output stream. ** @param socket The socket which's output * stream to write to ** @param b The byte array to write to the socket output stream ** @param * offset The start offset in the data to begin writing at ** @param length The length of the * data. Normally <code>b.length</code> ** @throws IOException if an error occurs */ protected static void socketWriteBytes(Socket socket, byte b[], int offset, int length) throws IOException { if ((socket != null) && (b != null)) { int bofs = offset; int blen = (length >= 0) ? length : b.length; OutputStream output = socket.getOutputStream(); output.write(b, bofs, blen); output.flush(); } }
public void connect(String user, String pass) { if (!connected) { try { socket = new Socket(domain, 4446); out = new PrintWriter(socket.getOutputStream(), true); // in = new BufferedReader(new InputStreamReader(socket.getInputStream())); in = new ObjectInputStream(socket.getInputStream()); } catch (java.net.UnknownHostException e) { System.err.println("Don't know about host"); return; } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to"); return; } this.connected = true; rec = new Receiver(in); rec.start(); } }
// {{{ handleClient() method private boolean handleClient(final Socket client, DataInputStream in) throws Exception { int key = in.readInt(); if (key != authKey) { Log.log( Log.ERROR, this, client + ": wrong" + " authorization key (got " + key + ", expected " + authKey + ")"); in.close(); client.close(); return false; } else { // Reset the timeout client.setSoTimeout(0); Log.log(Log.DEBUG, this, client + ": authenticated" + " successfully"); final String script = in.readUTF(); Log.log(Log.DEBUG, this, script); SwingUtilities.invokeLater( new Runnable() { public void run() { try { NameSpace ns = new NameSpace(BeanShell.getNameSpace(), "EditServer namespace"); ns.setVariable("socket", client); BeanShell.eval(null, ns, script); } catch (org.gjt.sp.jedit.bsh.UtilEvalError e) { Log.log(Log.ERROR, this, e); } finally { try { BeanShell.getNameSpace().setVariable("socket", null); } catch (org.gjt.sp.jedit.bsh.UtilEvalError e) { Log.log(Log.ERROR, this, e); } } } }); return true; } } // }}}
public ReciveFile() { super("파일전송"); setLayout(null); lbl = new Label("파일 전송을 기다립니다."); lbl.setBounds(10, 30, 230, 20); lbl.setBackground(Color.gray); lbl.setForeground(Color.white); add(lbl); txt = new TextArea("", 0, 0, TextArea.SCROLLBARS_BOTH); txt.setBounds(10, 60, 230, 100); txt.setEditable(false); add(txt); btn = new Button("닫기"); btn.setBounds(105, 170, 40, 20); btn.setVisible(false); btn.addActionListener(this); add(btn); addWindowListener(new WinListener()); setSize(250, 200); show(); try { ServerSocket socket = new ServerSocket(port); Socket sock = null; FileThread client = null; try { sock = socket.accept(); client = new FileThread(this, sock); client.start(); } catch (IOException e) { System.out.println(e); try { if (sock != null) sock.close(); } catch (IOException e1) { System.out.println(e1); } finally { sock = null; } } } catch (IOException e) { } }
public void connect() { try { s = new Socket("127.0.0.1", 8888); dos = new DataOutputStream(s.getOutputStream()); System.out.println("connected!"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
// メッセージをサーバーに送信する public void sendMessage(String msg) { try { OutputStream output = socket.getOutputStream(); PrintWriter writer = new PrintWriter(output); writer.println(msg); writer.flush(); } catch (Exception err) { msgTextArea.append("ERROR>" + err + "\n"); } }
// Close crap after chatting private void closeCrap() { showMessage("\n Closing connection... \n "); ableToType(false); try { output.close(); input.close(); connection.close(); } catch (IOException ioException) { ioException.printStackTrace(); } }
// Close socket and IO streams, change appearance/functionality of some components private void closeAll() throws IOException { displayArea.append("\nConnection closing"); output.close(); input.close(); connection.close(); // We are no longer connected connection = null; // Change components serverField.setEditable(true); connectButton.setLabel("Connect to server above"); enterField.setEnabled(false); }
@Override public void run() { // TODO Auto-generated method stub try { Socket s = new Socket(hostin, portin); InputStream ins = s.getInputStream(); OutputStream os = s.getOutputStream(); ir = new BufferedReader(new InputStreamReader(ins)); pw = new PrintWriter(new OutputStreamWriter(os), true); pw.print(nick); while (true) { String line = ir.readLine(); jta.append(line + "\n"); jsp.getVerticalScrollBar().setValue(jsp.getVerticalScrollBar().getMaximum()); } } catch (IOException e) { e.printStackTrace(); } }
public void disconnect() { int cmdID = commID++; this.connected = false; try { out.println(cmdID + ";logout"); out.flush(); in.close(); out.close(); socket.close(); } catch (IOException ex) { System.err.println("Server stop failed."); } }
public void run() { String texto = ""; while (repetir) { try { Empleados e = (Empleados) inObjeto.readObject(); textarea1.setText(""); textarea1.setForeground(Color.blue); if (e == null) { textarea1.setForeground(Color.red); PintaMensaje("<<EL EMPLEADO NO EXISTE>>"); } else { texto = "Empleado: " + e.getEmpNo() + "\n " + "Oficio: " + e.getOficio() + "\tApellido: " + e.getApellido() + "\n " + "Comisión: " + e.getComision() + "\tDirección: " + e.getDir() + "\n " + "Alta: " + e.getFechaAlt() + "\tSalario: " + e.getSalario() + "\n " + "Departamento: " + e.getDepartamentos().getDnombre(); textarea1.append(texto); } } catch (SocketException s) { repetir = false; } catch (IOException e) { e.printStackTrace(); repetir = false; } catch (ClassNotFoundException e) { e.printStackTrace(); repetir = false; } } try { socket.close(); System.exit(0); } catch (IOException e) { e.printStackTrace(); } }
// {{{ run() method public void run() { for (; ; ) { if (abort) return; Socket client = null; try { client = socket.accept(); // Stop script kiddies from opening the edit // server port and just leaving it open, as a // DoS client.setSoTimeout(1000); Log.log(Log.MESSAGE, this, client + ": connected"); DataInputStream in = new DataInputStream(client.getInputStream()); if (!handleClient(client, in)) abort = true; } catch (Exception e) { if (!abort) Log.log(Log.ERROR, this, e); abort = true; } finally { /* if(client != null) { try { client.close(); } catch(Exception e) { Log.log(Log.ERROR,this,e); } client = null; } */ } } } // }}}
@Override public void run() { try { // Create a server socket ServerSocket serverSocket = new ServerSocket(8000); textArea.append("TCP connection listener started at " + new Date() + '\n'); while (true) { // Listen for a new connection request Socket socket = serverSocket.accept(); // Display the client number textArea.append( "Starting thread for TCP client " + clientNo + " at " + new Date() + '\n'); // Find the client's host name, and IP address InetAddress inetAddress = socket.getInetAddress(); textArea.append( "Client " + clientNo + "'s host name is " + inetAddress.getHostName() + "\n"); textArea.append( "Client " + clientNo + "'s IP Address is " + inetAddress.getHostAddress() + "\n"); // Create a new thread for the TCP connection HandleTCPClient task = new HandleTCPClient(socket); // Start the new thread new Thread(task).start(); // Increment clientNo clientNo++; } } catch (IOException ex) { System.err.println(ex); } }