Java Socket Client Practice

Source: Internet
Author: User
Tags flush throwable

Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import java.io.IOException;
Import Java.net.Socket;
Import java.net.SocketException;
Import java.net.UnknownHostException;

Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;

public class Socketclient {
Protected final Static log = Logfactory.getlog (Socketclient.class);

private static final int receive_buffer_size = 102400;

Private Socket clientsocket = null;

Private String host = "127.0.0.1";

private int port = 6600;

Private Bufferedinputstream in;

Private Bufferedoutputstream out;

@SuppressWarnings ("unused")
Private String charset = "GBK";

Public Socketclient (String host, int port) {
This.host = host;
This.port = port;
}

/**
*
* Send Information
*
* @param bdata
* Name Thread Name
*/
public void Send (byte[] bdata, String name) {
try {
Log.debug ("Thread: + name +" Send data ...) ");
Out.write (bdata);
Out.flush ();
catch (Throwable ex) {
throw new RuntimeException (ex);
}
}

/**
* Establish socket connection
* Name Thread Name
* @throws Throwable
* @throws IOException
* @throws unknownhostexception
* @throws IOException
* @throws unknownhostexception
*/
public void Opensocket (String name) {
try {
Log.debug ("Thread:" + name + "Host:" + Host + "; Port:" + port + "establish connection");
Clientsocket = new Socket (host, Port);
in = new Bufferedinputstream (Clientsocket.getinputstream ());
out = new Bufferedoutputstream (Clientsocket.getoutputstream ());
catch (Throwable e) {
throw new RuntimeException ("Thread:" + name + "HOST: + Host +"; Port: "+ Port +" exception.) ", e);
}
}

/**
*
* Send Information
*
* @param bdata
*
*/
public void Send (byte[] bdata) {
try {
Out.write (bdata);
Out.flush ();
catch (Throwable ex) {
throw new RuntimeException (ex);
}
}

/**
* Establish socket connection
* Name Thread Name
*/
public void Opensocket () {
try {
Log.debug ("Main engine:" + host + "; Port:" "+ port +" establish connection ");
Clientsocket = new Socket (host, Port);
in = new Bufferedinputstream (Clientsocket.getinputstream ());
out = new Bufferedoutputstream (Clientsocket.getoutputstream ());
catch (Throwable e) {
throw new RuntimeException (e);
}
}

/**
* Disconnect Socket Connection
*/
public void Closesocket () {
if (clientsocket!= null) {
try {
Log.debug ("close socket");
Clientsocket.close ();
catch (Throwable e) {
throw new RuntimeException (e);
}
}
Clientsocket = null;
}

/**
* Name thread names
*
* Disconnect Socket Connection
*/
public void Closesocket (String name) {
if (clientsocket!= null) {
try {
Log.debug ("Thread:" + name + "close socket");
Clientsocket.close ();
catch (Throwable e) {
throw new RuntimeException (e);
}
}
Clientsocket = null;
}

/**
* Read and retrieve transmission information
*
* @return
* @throws Exception
* @throws SocketException
*/
Public byte[] Receive () throws Exception, SocketException {
byte rsp[] = new Byte[receive_buffer_size];
int iindex = 0;
if ((Clientsocket = null) | | (in = = null)) {
Log.debug ("Invalid set interface, unable to read data");
Log.error ("Invalid set interface, unable to read data");
throw new SocketException ("Invalid socket interface, unable to read data");
}
int itimeouttimes = 600;
int iavailable = 0;
while (true) {
iavailable = In.available ();

if (iavailable!= 0) {
for (int i = 0; i < iavailable; i++) {
int temp_data = In.read ();
if (Temp_data!=-1) {
Rsp[iindex + 0] = (byte) (Temp_data & 0X0FF);
iindex++;
if (Iindex >= receive_buffer_size) {
Log.debug ("Buffer length too small to continue receiving cm return results!");
Log.error ("Buffer length too small to continue receiving cm return results!");
throw new RuntimeException ("Buffer length too small to continue receiving cm return results!");
}
}
}
Break
}
Thread.Sleep (200);
itimeouttimes--;

            if (clientsocket = null)
                 break;
       }
        byte[] result = new Byte[iindex];
  for (int i = 0; i < Iindex i++) {
   result[i] = rsp[i];
  
&nb Sp; return result;
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.