Tag:socket server
Socket client = null;try{ client = new socket (Ip,Port); string msg= "Send the data content! "; //get socket read/write stream, send data to Server program client.getoutputstream () . Write (Msg.getbytes ()); byte[] datas = new byte[2048]; //receives data from the service-side program client.getinputstream (). Read (datas); System.out.println (new string (Datas));} catch (exception e) { e.printstacktrace ();} finally { if (client != null) { try { client.close (); } catch (ioexception e)  {     &NBsp; system.out.println ("Systemerr:" +e) ; } }}try{ client = new socket (); Socketaddress socketaddress = new inetsocketaddress (Ip,port); Client.connect (socketaddress, 3000); string msg= "Access the server to return content! "; //get socket read/write stream, send data to Server program client.getoutputstream () . Write (Msg.getbytes ()); byte[] datas = new byte[2048]; //receives data from the service-side program client.getinputstream (). Read (datas); System.out.println (new string (Datas));} catch (exception e) { e.printstacktrace ();} finally { if (client != null) { try { client.close (); } catch (ioexception e) { system.out.println ("Systemerr:" +e); } }}
Java sends and receives data through socket programming