Service side
Import Java.net.*;import java.io.*;p ublic class Tcpserversocket {public static void main (string[] args) throws exception{ String data = "";//while (True) {Try{serversocket SS = new ServerSocket (8888); Socket s = ss.accept (); InputStream in = S.getinputstream (); int len = 0;byte[] Buff = new Byte[1024];while (len = In.read (b UFF)) > 0) {data + = new String (Buff,0,len);}} catch (Exception e) {System.out.println ("Exception");} SYSTEM.OUT.PRINTLN (data);/}}}
Client
import java.net.*;import java.io.*;p ublic class tcpsocket{public static void main (String[] args) throws Exception{int port = 8888; string ip = "127.0.0.1";//inetaddress address = inetaddress.getbyaddress ( Stringtobyte (IP)); Inetaddress address = inetaddress.getbyname ("localhost"); Socket socket = new socket (Address, port);outputstream out = Socket.getoutputstream (); string data = "Hello"; byte buff[] = data.getbytes (); Out.write (buff,0,buff.length ); Out.flush (); Socket.close ();} public static byte[] stringtobyte (STRING&NBSP;IP) {string[] strarr = Ip.split ("."); byte[] iparr = new byte[4]; System.out.println (strarr.length); for (int i = 0; i < 4; i++) {Iparr[i] = (Byte) integer.parseint (Strarr[i]); }return iparr;}}
This article from "Endless life, toss more than" blog, please be sure to keep this source http://admxj.blog.51cto.com/10955090/1741400
Java Network Programming TCP case