Java Network Programming TCP case

Source: Internet
Author: User

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

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.