Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.client.core;
- import java.io.*;
- import java.net.*;
- public class SocketManager implements Runnable {
- private Socket sock;
- private PrintWriter output;
- private BufferedReader input;
- private String hostname;
- private int portnumber;
- public SocketManager(String ip, int port){
- try{
- sock = new Socket(ip,port);
- hostname = ip;
- portnumber = port;
- }catch(Exception e){
- System.out.println("Client: Socket failed to connect.");
- }
- }
- public synchronized void send(String data){
- try{
- output.println(data);
- output.flush();
- }catch(Exception e){
- }
- }
- public synchronized void connect(){
- try{
- sock = new Socket(hostname,portnumber);
- }catch(Exception e){
- }
- }
- public synchronized Socket getSocket(){
- return sock;
- }
- public synchronized void connect(String host, int port){
- try{
- sock = new Socket(host, port);
- }catch(Exception e){
- }
- }
- public synchronized void close(){
- try{
- sock.close();
- }catch(Exception e){
- }
- output = null;
- input = null;
- System.gc();
- }
- public void listenStream(){
- try {
- while(input.ready()){
- System.out.println(input.readLine());
- }
- } catch (Exception e) {
- }
- }
- @Override
- public void run() {
- try{
- output = new PrintWriter(sock.getOutputStream());
- input = new BufferedReader(
- new InputStreamReader(sock.getInputStream()));
- }catch(Exception e){
- }
- listenStream();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement