Multithreaded +socket for Java

Source: Internet
Author: User

Client:

Package Com.wulala;

Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.Socket;
Import java.net.UnknownHostException;

public class Client {
public static void Main (string[] args) {
Client client = new Client ();
Client.run ();

}

public void Run () {
Socket client;
try {
Client = new Socket ("127.0.0.1", 9999);
2. Open the flow information for the socket and prepare the following actions
OutputStream OS = Client.getoutputstream ();
3. Write a message
Os.write (("xxxxasdf12233"). GetBytes ());

}
} catch (Unknownhostexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

Parentheses may not be complete and unimportant.

The client has nothing to say, is to open a target server an IP, followed the port, and then outputsteam some things into, analog back STM32 through the GPRS connection server through the serial port through the transmission of data.

Server-side:

Package Com.wulala;

Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;

public class Multisocketserver {
public static void Main (String args[]) {
Multisocketserver multisocketserver = new Multisocketserver ();
try {
Multisocketserver.startserver ();
} catch (Exception ex) {

System.out.println (Ex.getmessage ());
}

}

public void StartServer () throws IOException {
ServerSocket SS = null;
try {
SS = new ServerSocket (9999);
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
while (true) {
1. Open the Socket connection
Waiting for a client request
Final Socket server = Ss.accept ();

SYSTEM.OUT.PRINTLN ("Service-----------request starts start");

2. Open the flow information for the socket and prepare the following actions
Final InputStream is = Server.getinputstream ();
byte b[] = new byte[1024];

int readcount = Is.read (b);

String str = new string (b);

str = Str.trim ();

Final String serverfilename = str;

if (Readcount > 0) {
New Thread () {

@Override
public void Run () {

SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "start");
3. Stream information for read and write operations
SYSTEM.OUT.PRINTLN ("The message sent by the client is:" + serverfilename);

Executedml edml = new Executedml ();
try {
Edml.initparam ("Dbconfig.properties");
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
int insertresult = 0;

String DeviceID = "";
int activate = 0;
if (Serverfilename.length () > 9) {
DeviceID = serverfilename.substring (0, 8);
activate = Integer.valueof (serverfilename.substring (8));
}
try {
Insertresult = Edml.insertwxdata ("AAAA", 113);
Insertresult = Edml.insertwxdata (DeviceID, activate);
SYSTEM.OUT.PRINTLN ("Total" +insertresult+ "result been changed");
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
Is.close ();
Server.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Return
}
}.start ();
}

SYSTEM.OUT.PRINTLN ("Service-----------request end over");
}
}
}

To put it simply, a thread is used when the data comes up, and in this project, a string is usually sent over, and the first 8 bits of the string are the device name, followed by the number of activations. or something.

In the future to receive data validation, analysis of what, the late reconstruction bar.

Multithreaded +socket for Java

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.