package com.server;
import java.io.*;
import java.net.*;
class Myserver extends Thread // Myserver 一定要定义为 线程吗??? 不定义为线程,而把子线程定义行不行???
{
ServerSocket ss=null;
Socket s1=null;
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Myserver ms= new Myserver();
ms.start();
// 当你定义了线程时,必须要启用线程。
// 只有调用对象的 start() 方法时,线程才会被执行。
}
//构造方法
public Myserver()
{
try {
ss=new ServerSocket(9999);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run()
{
// TODO Auto-generated method stub
// 开启其他进程
for(int n=1;n<3;n++) // 我这里通过设置n ,来达到控制服务器所能接收的最大连接数。
{
try {
// 创建服务器套接字
// 让服务器处于监听状态
Socket s=ss.accept();
new Subprocess(s,n).start();
// temp.start()报错:The method start() is undefined for the type Myserver.Subprocess
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 定义一个子线程类
class Subprocess extends Thread
{
// 子线程要做的工作就是:
/*1.创建一个已连接套接字
* 2.负责处理来自某一特定的已连接套接字
*
*
*
* */
// 为子线程 定义属性
/*子线程都要有哪些属性呢??
* 1.Socket
*
*
*
* */
Socket sub_socket=null;
// 定义一个线程序号
int n;
String cont=null;
Subprocess(Socket socket,int m)
{
this.sub_socket=socket;
this.n=m;
}
@Override
public void run()
{
// 开辟套接字流
DataInputStream dis=null;
DataOutputStream dos=null;
try {
dis=new DataInputStream (this.sub_socket.getInputStream());
cont = dis.readUTF();
System.out.println("线程序号是: "+n);
System.out.println(cont);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
class Myserver extends Thread // Myserver 一定要定义为 线程吗??? 不定义为线程,而把子线程定义行不行???
{
ServerSocket ss=null;
Socket s1=null;
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Myserver ms= new Myserver();
ms.start();
// 当你定义了线程时,必须要启用线程。
// 只有调用对象的 start() 方法时,线程才会被执行。
}
//构造方法
public Myserver()
{
try {
ss=new ServerSocket(9999);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run()
{
// TODO Auto-generated method stub
// 开启其他进程
for(int n=1;n<3;n++) // 我这里通过设置n ,来达到控制服务器所能接收的最大连接数。
{
try {
// 创建服务器套接字
// 让服务器处于监听状态
Socket s=ss.accept();
new Subprocess(s,n).start();
// temp.start()报错:The method start() is undefined for the type Myserver.Subprocess
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 定义一个子线程类
class Subprocess extends Thread
{
// 子线程要做的工作就是:
/*1.创建一个已连接套接字
* 2.负责处理来自某一特定的已连接套接字
*
*
*
* */
// 为子线程 定义属性
/*子线程都要有哪些属性呢??
* 1.Socket
*
*
*
* */
Socket sub_socket=null;
// 定义一个线程序号
int n;
String cont=null;
Subprocess(Socket socket,int m)
{
this.sub_socket=socket;
this.n=m;
}
@Override
public void run()
{
// 开辟套接字流
DataInputStream dis=null;
DataOutputStream dos=null;
try {
dis=new DataInputStream (this.sub_socket.getInputStream());
cont = dis.readUTF();
System.out.println("线程序号是: "+n);
System.out.println(cont);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.client;
import java.io.*;
import java.net.*;
class Myclient
{
/**
* @param args
*/
DataOutputStream dos=null;
String cont=null;
public static void main(String[] args)
{
// TODO Auto-generated method stub
new Myclient();
System.out.println("大家好");
}
public Myclient()
{
// 建立连接请求
try {
Socket s=new Socket("127.0.0.1",9999);
// 构造套接字流
dos=new DataOutputStream(s.getOutputStream());
cont="你好,服务器。。";
dos.writeUTF(cont);
dos.flush();
dos.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}