java 滚动条无反应_滚动条 的问题

这是有关于UDP通讯的例子

/*

* Receive.java

*

* Created on 2006年11月29日, 下午8:56

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package testUDP;

/**

*

* @author lbf

*/

import java.net.*;

import java.io.*;

public class Receive extends Thread{

private DatagramSocket ds;

private DatagramPacket dp;

/** Creates a new instance of Receive */

public Receive() {

initOther();

this.start();

}

private void initOther(){

try{

ds=new DatagramSocket(5000);

dp=new DatagramPacket(new byte[8192],8192);

}

catch(Exception exe){

exe.printStackTrace();

}

}

private void displayPacket(){

String s=new String(dp.getData(),0,dp.getLength());

String ip=dp.getAddress().getHostAddress();

int port=dp.getPort();

System.out.println(s+" from "+ip+":"+port);

}

public void run(){

System.out.println("start receive.....");

while(true){

try{

ds.receive(dp);

displayPacket();

}

catch(Exception exe){

exe.printStackTrace();

}

}

}

public static void main(String[] args) {

new Receive();

}

}

/*

* Send.java

*

* Created on 2006年11月29日, 下午8:56

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package testUDP;

/**

*

* @author lbf

*/

import java.io.*;

import java.net.*;

public class Send extends Thread{

private DatagramSocket ds;

private DatagramPacket dp;

/** Creates a new instance of Send */

public Send() {

initOther();

this.start();

}

private void initOther(){

try{

byte[] data="Hello World!".getBytes();

InetAddress to=InetAddress.getLocalHost();

ds=new DatagramSocket();

dp=new DatagramPacket(data,data.length,to,5000);

}

catch(Exception exe){

exe.printStackTrace();

}

}

private void sendPacket()throws Exception{

ds.send(dp);

System.out.println("send over");

}

public void run(){

System.out.println("start to send.....");

while(true){

try{

sendPacket();

Thread.sleep(1000);

}

catch(Exception exe){

exe.printStackTrace();

}

}

}

public static void main(String[] args) {

new Send();

}

}

============================================================================================

以下是关于TCP通讯的例子,你好好看吧

/*

* Receive.java

*

* Created on 2006年12月19日, 下午2:31

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package testTCP;

/**

*

* @author lbf

*/

import java.io.*;

import java.net.*;

public class Receive{

/** Creates a new instance of Receive */

public Receive() {

listen();

}

private void listen(){

try{

ServerSocket ss=new ServerSocket(10000);

while(true){

Socket s=ss.accept();

new HandleConnect(s).start();

}

}

catch(Exception exe){

exe.printStackTrace();

}

}

private class HandleConnect extends Thread{

private BufferedReader br;

public HandleConnect(Socket s){

try{

br=new BufferedReader(new InputStreamReader(s.getInputStream()));

}

catch(Exception exe ){

exe.printStackTrace();

}

}

public void run(){

while(true){

try{

System.out.println("收到的信息:"+br.readLine());

}

catch(Exception exe ){

exe.printStackTrace();

break;

}

}

}

}

public static void main(String[] args) {

new Receive();

}

}

/*

* Send.java

*

* Created on 2006年12月19日, 下午2:31

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package testTCP;

/**

*

* @author lbf

*/

import java.io.*;

import java.net.*;

public class Send extends Thread{

private PrintWriter pw;

/** Creates a new instance of Send */

public Send(String name) {

super(name);

initSocket();

this.start();

}

private void initSocket(){

try{

Socket s=new Socket("localhost",10000);

pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

}

catch(Exception exe ){

exe.printStackTrace();

}

}

public void run(){

while(true){

try{

Thread.sleep(1000);

pw.println(this.getName()+"这是通过TCP发过去的信息!");

pw.flush();

}catch(Exception exe){

exe.printStackTrace();

break;

}

}

}

public static void main(String[] args) {

new Send("线程1");

}

}

都是先启动Receive,再启动Send

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值