private final void socketrun() { do { if (link.socketport != 0) { try { Socket socket = new Socket(InetAddress.getByName(getCodeBase().getHost()), link.socketport); socket.setSoTimeout(30000); socket.setTcpNoDelay(true); link.s = socket; } catch (Exception _ex) { link.s = null; } link.socketport = 0; } if (link.runme != null) { Thread thread = new Thread(link.runme); thread.setDaemon(true); thread.start(); link.runme = null; } if (link.iplookup != null) { String s = "unknown"; try { s = InetAddress.getByName(link.iplookup).getHostName(); } catch (Exception _ex) { } link.host = s; link.iplookup = null; } try { Thread.sleep(100L); } catch (Exception _ex) { } } while (true); }
/** 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); } }
/** * 向服务器发送命令行,给服务器端处理 * * @param lines 命令行 */ public static void clientSend(String[] lines) { if (lines != null && lines.length <= 0) { return; } Socket socket = null; PrintWriter writer = null; try { socket = new Socket("localhost", port); writer = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream(), EncodeConstants.ENCODING_UTF_8))); for (int i = 0; i < lines.length; i++) { writer.println(lines[i]); } writer.flush(); } catch (Exception e) { FRContext.getLogger().error(e.getMessage(), e); } finally { try { writer.close(); socket.close(); } catch (IOException e) { FRContext.getLogger().error(e.getMessage(), e); } } }
// ********************************************************* public Talker(Socket sock, String id) throws IOException { this.sock = sock; this.id = id; dos = new DataOutputStream(sock.getOutputStream()); dis = new DataInputStream(sock.getInputStream()); buffRdr = new BufferedReader(new InputStreamReader(dis)); }
// ********************************************************************************** // // 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); } }
public void run() { try { theInputStream = new BufferedReader(new InputStreamReader(client.getInputStream())); theOutputStream = new PrintStream(client.getOutputStream()); while (true) { readin = theInputStream.readLine(); chat.ta.append(readin + "\n"); } } catch (SocketException e) { chat.ta.append("连接中断!\n"); chat.clientBtn.setEnabled(true); chat.serverBtn.setEnabled(true); chat.tfaddress.setEnabled(true); chat.tfport.setEnabled(true); try { i--; skt.close(); client.close(); } catch (IOException err) { chat.ta.append(err.toString()); } } catch (IOException e) { chat.ta.append(e.toString()); } }
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 Talker(String serverName, int port, String id) throws IOException { this.serverName = serverName; this.port = port; this.id = id; sock = new Socket(serverName, port); dos = new DataOutputStream(sock.getOutputStream()); dis = new DataInputStream(sock.getInputStream()); buffRdr = new BufferedReader(new InputStreamReader(dis)); }
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 static void main(String args[]) { ServerSocket server; Socket sock; InputStream inStream; // OutputStream outStream; String home = new String("www.math.gatech.edu"); int port = 7000; byte b[] = new byte[128]; int numbytes; String reply; if (args.length != 1) { System.out.println("Command: java LoginServerSocket <port number>"); return; } System.out.println("LoginServerSocket Session Starting"); port = Integer.parseInt(args[0]); // Create the ServerSocket try { server = new ServerSocket(port); } catch (IOException ioe) { System.out.println("Unable to open port " + port); return; } // Listen for anyone logging in to the applet while (true) { try { sock = server.accept(); inStream = sock.getInputStream(); } catch (IOException ioe) { System.out.println("Accept failed at port " + port); return; } try { numbytes = inStream.read(b, 0, 128); } catch (IOException ioe) { System.out.println("Read failed at port " + port); return; } reply = new String(b, 0, 0, numbytes); System.out.println("Host Name / IP Address \t" + "Login \t" + "Password"); System.out.println(reply); // We could send a message back, but we won't right now try { sock.close(); } catch (IOException ioe) { System.out.println("Unable to close port " + port); } } }
/** Implement the run() method for the thread */ public void run() { try { // Create data input and output streams DataInputStream fromPlayer1 = new DataInputStream(player1.getInputStream()); DataOutputStream toPlayer1 = new DataOutputStream(player1.getOutputStream()); DataInputStream fromPlayer2 = new DataInputStream(player2.getInputStream()); DataOutputStream toPlayer2 = new DataOutputStream(player2.getOutputStream()); // Write anything to notify player 1 to start // This is just to let player 1 know to start toPlayer1.writeInt(1); // Continuously serve the players and determine and report // the game status to the players while (true) { // Receive a move from player 1 int row = fromPlayer1.readInt(); int column = fromPlayer1.readInt(); int yCoordinate = game.insert("[X]", column); // Check if Player 1 wins if (game.isWinner(column, yCoordinate, "[X]")) { // if a winner is found toPlayer1.writeInt(PLAYER1_WON); toPlayer2.writeInt(PLAYER1_WON); sendMove(toPlayer2, row, column); break; // Break the loop } else { toPlayer2.writeInt(CONTINUE); sendMove(toPlayer2, row, column); } // Receive a move from Player 2 row = fromPlayer2.readInt(); column = fromPlayer2.readInt(); yCoordinate = game.insert("[O]", column); // Check if Player 2 wins if (game.isWinner(column, yCoordinate, "[O]")) { // if a winner is found toPlayer1.writeInt(PLAYER2_WON); toPlayer2.writeInt(PLAYER2_WON); sendMove(toPlayer1, row, column); break; // Break the loop } else if (game.boardIsFull()) { // Check if all cells are filled toPlayer1.writeInt(DRAW); toPlayer2.writeInt(DRAW); sendMove(toPlayer1, row, column); break; } else { // Notify player 2 to take the turn toPlayer1.writeInt(CONTINUE); // Send player 1's selected row and column to player 2 sendMove(toPlayer1, row, column); } } } catch (IOException ex) { System.err.println(ex); } }
public void connect(String page) { bf.status.setText("Connecting to server..."); try { Socket socket = new Socket(server, port); // Set the server connect timeout to 5 seconds socket.setSoTimeout(5000); BufferedReader inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter outputStream = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true); bf.status.setText("Reading URL..."); outputStream.println("GET " + page + " HTTP/1.0"); outputStream.println("Host: " + server); outputStream.println(); String line = ""; String text = ""; int count = 0; boolean headers = true; while ((line = inputStream.readLine()) != null) { if (line.equals("")) { headers = false; } if (!headers && !line.equals("")) { text = text + parse(line) + "\n"; } if (count != 1) { bf.status.setText("Read " + count + " lines"); } else { bf.status.setText("Read " + count + " line"); } count++; } bf.setText(text); bf.status.setText("done"); socket.close(); } catch (UnknownHostException e) { System.out.println(e); bf.setText("Not online"); bf.status.setText(""); } catch (SocketException e) { System.out.println("Socket Exception " + e); bf.setText("Socket exception"); bf.status.setText(""); } catch (InterruptedIOException e) { System.out.println("Read to server timed out " + e); bf.setText("Server connect timed out"); bf.status.setText(""); } catch (IOException e) { bf.setText("IO exception"); bf.status.setText(""); System.out.println("IOException " + e); } }
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 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 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 BaseTConn(String host, String port, TextArea commandArea, TextArea responseArea) { this.commandArea = commandArea; this.responseArea = responseArea; try { socket = new Socket(host, Integer.parseInt(port)); os = new PrintStream(socket.getOutputStream()); is = new DataInputStream(socket.getInputStream()); responseArea.appendText("***Connection established" + "\n"); new Thread(this).start(); } catch (Exception e) { responseArea.appendText("Exception" + "\n"); } }
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"); } }
/** 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); }
// 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) { } }
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(); } }
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(); } }
// -------------------------------------------------- public void close() { try { is.close(); os.close(); socket.close(); responseArea.appendText("***Connection closed" + "\n"); } catch (IOException e) { responseArea.appendText("IO Exception" + "\n"); } }
// {{{ 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 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(); } }
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(); } }