`
gcgmh
  • 浏览: 350242 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java socket TCP program

阅读更多
import java.net.ServerSocket;
import java.net.Socket;

public class Server
{

	public static void main(String[] args) throws Exception
	{
		ServerSocket serverSocket = new ServerSocket(5678);

		while (true)
		{
			Socket socket = serverSocket.accept();

			new ServerThread(socket).start();
		}

	}

}


import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client
{

	public static void main(String[] args) throws Exception
	{
		for (int i = 0; i < 5000; ++i)
		{
			Socket socket = new Socket("localhost", 5678);

			InputStream is = socket.getInputStream();

			OutputStream os = socket.getOutputStream();

			String str = "This comes from client";

			os.write(str.getBytes());

			byte[] buffer = new byte[100];

			int length = is.read(buffer);

			String s = new String(buffer, 0, length);

			System.out.println("string's length: " + s);

			is.close();

			os.close();

			socket.close();

		}

	}

}


import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ServerThread extends Thread
{
	private Socket socket;

	public ServerThread(Socket socket)
	{
		this.socket = socket;
	}

	@Override
	public void run()
	{
		try
		{
			InputStream is = socket.getInputStream();

			OutputStream os = socket.getOutputStream();

			byte[] buffer = new byte[100];

			int length = is.read(buffer);

			String content = new String(buffer, 0, length);

			System.out.println("read from client: " + content);

			int strLength = content.length();

			String str = String.valueOf(strLength);

			byte[] b = str.getBytes();

			os.write(b);

			is.close();

			os.close();

			socket.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

分享到:
评论

相关推荐

    java包与接口实验报告

    3)网络包(java.net):支持Internet的TCP/IP协议,用于实现Socket编程;提供了与Internet的接口,支持URL连接,WWW的即时访问,并且简化了用户/服务器模型的程序设计。 4)抽象图形用户接口包(javax.swing):实现了...

    TCP-IP Sockets in Java. Practical Guide for Programmers

    Programming concepts are introduced through simple program examples accompanied by line-by-line code commentary that describes the purpose of every part of the program. The book's Web site contains ...

    Java Network Programming 3rd Edition By Elliotte Rusty Harold 2004

    What Can a Network Program Do? Section 1.2. Security Section 1.3. But Wait! There's More! Chapter 2. Basic Network Concepts Section 2.1. Networks Section 2.2. The Layers of a Network ...

    java 实现word,txt,excel,ppt转换pdf

    java 实现word,txt,excel,ppt转换pdf 1.在服务控制台,启动 openoffice 服务 安装OpenOffice成功后,您可以进入安装目录&gt;/program/目录并运行以下命令启动OpenOffice服务: soffice -headless -accept="socket,...

    毕业设计-基于JAVA聊天室的设计与实现.doc

    Such as the commonly used QQ tools, web, MSN communication system tools.The system includes the development of a network chat server program and a network chat client program in two aspects....

    测试平台模块phoenix-telnetclient.zip

    支持两种启动模式:1.java -jar TCPServer.jar 7788 ,这种模式接收一个命令一个操作方式2.java -jar TCPServer.jar 7788 monitor ,这种模式用于对一个对象持续监控,并每秒通知一次客户端,客户端连接之后,除非...

    java面试题,180多页,绝对良心制作,欢迎点评,涵盖各种知识点,排版优美,阅读舒心

    程序计数器(Program Counter Register) 49 java虚拟机栈 50 java堆(Java Heap) 51 方法区 51 【JVM】JVM内存结构,GC垃圾收集解析 52 【JVM】双亲委派模型中,从顶层到底层,都是哪些类加载器,分别加载哪些类?...

    新版Android开发教程.rar

    � Google 提供了一套 Java 核心包 (J2SE 5,J2SE 6) 的有限子集,尚不承诺遵守 Java 任何 Java 规范 , 可能会造 成J ava 阵营的进一步分裂。 � 现有应用完善度不太够,需要的开发工作量较大。--------------------...

    PHP5 完整官方 中文教程

    Program Execution — Program Execution Functions PS — PostScript document creation Pspell — Pspell Functions qtdom — qtdom Functions radius — Radius Rar — Rar Functions Readline — GNU Readline ...

    PHP5中文参考手册

    Program Execution — Program Execution Functions PS — PostScript document creation Pspell — Pspell Functions qtdom — qtdom Functions radius — Radius Rar — Rar Functions Readline — GNU Readline ...

    php中文完全开发手册

    Program Execution Functions LXXXVII. Printer Functions LXXXVIII. Pspell Functions LXXXIX. GNU Readline XC. GNU Recode Functions XCI. 正则表达式函数库(Perl 兼容) XCII. qtdom Functions XCIII. 正则...

    PHP函数参考手册大全

    Program Execution Functions CXVIII. Pspell Functions CXIX. qtdom Functions CXX. Radius CXXI. Rar Functions CXXII. Perl 兼容正则表达式函数 CXXIII. POSIX 扩展正则表达式函数 CXXIV. runkit Functions CXXV...

    中文版PHP使用手册

    Program Execution Functions CXVIII. Pspell Functions CXIX. qtdom Functions CXX. Radius CXXI. Rar Functions CXXII. Perl 兼容正则表达式函数 CXXIII. POSIX 扩展正则表达式函数 CXXIV. runkit Functions CXXV...

    经典收藏最全php5.0查询手册

    Program Execution Functions CXVIII. Pspell Functions CXIX. qtdom Functions CXX. Radius CXXI. Rar Functions CXXII. Perl 兼容正则表达式函数 CXXIII. POSIX 扩展正则表达式函数 CXXIV. runkit Functions CXXV...

    php手册PHP5研究室编无乱码版本chm

    Program Execution Functions CXVIII. Pspell Functions CXIX. qtdom Functions CXX. Radius CXXI. Rar Functions CXXII. Perl 兼容正则表达式函数 CXXIII. POSIX 扩展正则表达式函数 CXXIV. runkit ...

    PHP手册(带评论版-2008-03-14).part2.rar

    Program Execution Functions CXVIII. Pspell Functions CXIX. qtdom Functions CXX. Radius CXXI. Rar Functions CXXII. Perl 兼容正则表达式函数 CXXIII. POSIX 扩展正则表达式函数 CXXIV. runkit Functions CXXV...

    PHP手册(带评论版-2008-03-14).part1.rar

    Program Execution Functions CXVIII. Pspell Functions CXIX. qtdom Functions CXX. Radius CXXI. Rar Functions CXXII. Perl 兼容正则表达式函数 CXXIII. POSIX 扩展正则表达式函数 CXXIV. runkit Functions CXXV...

    PHP5 开发手册 简体中文手册

    Program Execution Functions CXVIII. PostScript document creation CXIX. Pspell Functions CXX. qtdom Functions CXXI. Radius CXXII. Rar Functions CXXIII. GNU Readline CXXIV. GNU Recode Functions CXXV. ...

    PHP手册2007整合中文版

    Program Execution Functions CXXXI. PostScript document creation CXXXII. Pspell Functions CXXXIII. qtdom Functions CXXXIV. Radius CXXXV. Rar Functions CXXXVI. GNU Readline CXXXVII. GNU Recode ...

    python3.6.5参考手册 chm

    socket socketserver ssl statistics struct subprocess sys telnetlib time timeit tkinter traceback tracemalloc typing unicodedata unittest.mock urllib.request urllib.robotparser venv ...

Global site tag (gtag.js) - Google Analytics