Communication in Java based on a socket. A double unilateral direction communication example

xiaoxiao2021-03-06  64

/ * * Date: zhangyu6050; * Todo: A double unilateral direction communication example * This file is a host member (Server) * Provides a host and client communication set: fromClient * Bugs: * /

Import java.net. *; import java.io. *;

Public class chatserver {

Public static void main (string [] args) {final int port = 1775; // Establish communication host port number serverSocket Socserver = null; socket fromclient = null; // Host communication jacking with client: fromClient String Message = "Hello, I'm Server!"; try {socserver = new serversocket (port);} catch (ooException e) {system.err.println ("Could Not Listen on Port: port); System.exit (1); }

Socket clientSocket = null; try {fromClient = socServer.accept ();} catch (IOException e) {System.err.println ( "Accept failed."); System.exit (1);} try {ObjectOutputStream oos = new ObjectOutputStream (fromClient.getputstream ()); OOS.WriteObject (Message); OOS.Close (); fromClient.close (); // Turn off all stream connections before // clockworthop} catch (ioException E1) {E1.PrintStackTrace ();

转载请注明原文地址:https://www.9cbs.com/read-86554.html

New Post(0)