Dgram类

package Socket;

import java.net.DatagramPacket;
import java.net.InetAddress; public class Dgram { public static DatagramPacket toDatagram(String s, InetAddress destIA,
int destPort) {
byte[] buf = new byte[s.length() + 1];
s.getBytes(0, s.length(), buf, 0);
return new DatagramPacket(buf, buf.length, destIA, destPort);
} public static String toString(DatagramPacket p) {
return new String(p.getData(), 0, p.getLength());
}
}
MultiJabberServer
package Socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket; class ServeOneJabber extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out; public ServeOneJabber(Socket s) throws IOException {
socket = s;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
start();
} public void run() {
try {
while (true) {
String str = in.readLine();
if (str.equals("END"))
break;
System.out.println("Echoing:" + str);
out.println("From Server:"+str);
}
System.out.println("closing...");
} catch (IOException e) {
} finally {
try {
socket.close();
} catch (Exception e2) {
}
}
}
} public class MultiJabberServer {
static final int PORT = 8080; public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server Started");
try {
while (true) {
Socket socket = s.accept();
try {
new ServeOneJabber(socket);
} catch (IOException e) {
socket.close();
}
}
} finally {
s.close();
}
} }
MultiJabberClient
package Socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket; class JabberClientThread extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private static int counter = 0;
private int id = counter++;
private static int threadcount = 0; public static int threadCount() {
return threadcount;
} public JabberClientThread(InetAddress addr) {
System.out.println("Making client " + id);
threadcount++;
try {
socket = new Socket(addr, MultiJabberServer.PORT);
} catch (IOException e) {
}
try {
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
start();
} catch (IOException e) {
try {
socket.close();
} catch (IOException e2) {
}
}
} public void run() {
try {
for (int i = 0; i < 25; i++) {
out.println("Client " + id + ":" + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} catch (IOException e) {
} finally {
try {
socket.close();
} catch (IOException e2) {
threadcount--;
}
}
}
} public class MultiJabberClient {
static final int MAX_THREADS = 40; public static void main(String[] args) throws IOException,
InterruptedException {
InetAddress addr = InetAddress.getByName(null);
while (true) {
if (JabberClientThread.threadCount() < MAX_THREADS)
new JabberClientThread(addr);
Thread.currentThread().sleep(100);
}
}
}

java网络编程之TCP实例的更多相关文章

  1. Java网络编程之TCP、UDP

    Java网络编程之TCP.UDP 2014-11-25 15:23 513人阅读 评论(0) 收藏 举报 分类: java基础及多线程(28) 版权声明:本文为博主原创文章,未经博主允许不得转载.   ...

  2. Java网络编程之TCP

    Java网络编程之TCP ​ TCP主要需要两个类:Socket和ServerSocket,Socket是客户端连接服务器时创建,参数需要指定服务器的ip和端口,ServerSocket是服务器端创建 ...

  3. java网络编程之TCP通讯

    java中的网络编程之TCP协议的详细介绍,以及如何使用,同时我在下面举2例说明如何搭配IO流进行操作, /* *TCP *建立连接,形成传输数据的通道: *在连接中进行大数据量传输: *通过三次握手 ...

  4. Java网络编程之TCP通信

    一.概述 Socket类是Java执行客户端TCP操作的基础类,这个类本身使用代码通过主机操作系统的本地TCP栈进行通信.Socket类的方法会建立和销毁连接,设置各种Socket选项. Server ...

  5. java 网络编程之TCP通信和简单的文件上传功能

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  6. 4.Java网络编程之TCP/UDP

    常见传输协议: UDP , TCP UDP协议:    特点:         1.将数据及源和目的封装成数据包中,不需要建立连接         2.每个数据包的大小限制在64K内         ...

  7. java网络编程之UDP实例

    package Socket; import java.net.DatagramPacket; import java.net.InetAddress; public class Dgram { pu ...

  8. Java网络编程之InetAddress浅析

    Java网络编程之InetAddress浅析 一.InetAddress综述 IP地址是IP使用的32位(IPv4)或者128位(IPv6)位无符号数字,它是传输层协议TCP,UDP的基础.InetA ...

  9. Python网络编程之TCP套接字简单用法示例

    Python网络编程之TCP套接字简单用法示例 本文实例讲述了Python网络编程之TCP套接字简单用法.分享给大家供大家参考,具体如下: 上学期学的计算机网络,因为之前还未学习python,而jav ...

随机推荐

  1. 论文笔记之:Pedestrian Detection aided by Deep Learning Semantic Tasks

    Pedestrian Detection aided by Deep Learning Semantic Tasks CVPR 2015 本文考虑将语义任务(即:行人属性和场景属性)和行人检测相结合, ...

  2. lua for循环

    <转自网络,仅供学习> for循环是一个循环控制结构,可以有效地编写需要执行的特定次数的循环. 语法 Lua编程语言的for循环的语法是: for init,max/min value, ...

  3. Choosing proper innodb_log_file_size

    If you’re doing significant amount of writes to Innodb tables decent size of innodb_log_file_size is ...

  4. 伪类选择器:root的妙用

    css3的元素旋转功能非常强大,也非常吸引人,但是很多时候因为浏览器使用率的问题,我们必需要想办法兼容一些低版本的浏览器,特别是ie这朵奇葩. 想要实现元素旋转本来很简单的一个属性就能实现,那就是tr ...

  5. 【Reporting Services 报表开发】— 级联式参数设置

    级联式参数设置 再清楚的菜单,只要遇到选择项目一多的时候,难免会让人眼花缭乱,而找不到该选的选项.举例来说,像是零售业动辄万种商品品类,如果希望快速的选择到希望查看的产品品类时,就需要更有效率的搜索方 ...

  6. C# 常见集合之前的转换

    1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...

  7. encodeURI(encodeURI(name)) ;文件上传

    window.location.href = xxxx?a=encodeURI(encodeURI(name)) ;// 编码name是中文,页面部分需要编码两次name = java.net.URL ...

  8. 【jmeter】HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults

    Test Plan的配置元件中有一些和HTTP属性相关的元件:HTTP Cache Manager.HTTP Authorization Manager.HTTP Cookie Manager.HTT ...

  9. JQUERY 拖拽 draggable droppable resizable selectable sortable

    今天用了jq ui的拖动碰撞功能,好不容易看到有详细的API解说,记录如下:   <script language="JavaScript" type="text/ ...

  10. WINDOWS黑客基础(6):查看文件里面的导入表

    int main(void) { HANDLE hFile = CreateFile("D:\\Shipyard.exe", GENERIC_READ, FILE_SHARE_RE ...