Flex and Java Socket Communication (ii) one-way communication

Source: Internet
Author: User

Purpose: The client sends a message to the server, and the server displays the message on the screen without sending any data to the client.

The server will repeat the process.

1 Waiting for Client connection requests

2 Connecting Clients

3 Receive a string before disconnecting the client

4 Disconnect client Connections

5 Back to first step

Gets the input stream for the socket connected to the client, as follows.

Socket socket=server.accept(); //返回与客户机连接的套接字
InputStream is=socket.getInputStream();  //获取套接字的InputStream
InputStreamReader isr=new InputStreamReader(is);
BufferedReader reader= new BufferedReader(isr); //字符串流

Service-side code: Server2.java

=========================================

Import java.net.*;
Import java.io.*;
public class Server2 {
Private BufferedReader reader; Responsible for input
Private ServerSocket server; Server sockets
private socket socket; Sockets
Public Server2 () {}//default constructor
void StartServer ()//Start server
{
Try
{
Server=new ServerSocket (8888); Create a server socket
SYSTEM.OUT.PRINTLN ("Server socket set up");
while (true)
{
System.out.println ("Waiting for Client");
Socket=server.accept (); If the client requests a connection, connect to the socket
SYSTEM.OUT.PRINTLN ("Complete connection to client");
Get socket input stream, "utf-8" This encoding setting is to better display Chinese
Reader=new BufferedReader (New InputStreamReader (Socket.getinputstream (), "UTF-8"));
GetMessage ()//Read data from the client and output to the screen
}
}catch (Exception e)
{
System.out.println (e);
}
}
void GetMessage ()//read information from sockets
{
Try
{
while (true)//loop
{
SYSTEM.OUT.PRINTLN ("Client:" +reader.readline ());
}
}catch (Exception e) {}
finally{
SYSTEM.OUT.PRINTLN ("Client Disconnected");
Try
{
if (reader!=null) reader.close (); Close the input stream for a socket
if (socket!=null) socket.close (); Close socket
Reader=null;
Socket=null;
}catch (Exception e) {}
}
}
public static void Main (string[] args)
{
Server2 server=new Server2 ();
Server.startserver ();
}
}

======================================

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.