码迷,mamicode.com
首页 > 编程语言 > 详细

Socket多线程编程 serversocket服务器多线程编程

时间:2015-08-26 20:05:50      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

Socket多线程编程 serversocket服务器多线程编程

服务器代码

package com.lotus.server;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class SocketServer {
List<Socket> list;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SocketServer socketServer=new SocketServer();
socketServer.startServer();
if(Thread.activeCount()>1){
Thread.yield();
}


}



public void startServer(){
ServerSocket serverSocket=null;
try {
list=new ArrayList<Socket>();
serverSocket=new ServerSocket(9898);
new MeldSocket(serverSocket).start();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

class MeldSocket extends Thread{
private ServerSocket serverSocket;
public MeldSocket(ServerSocket serverSocket){
this.serverSocket=serverSocket;
}

@Override
public void run() {
// TODO Auto-generated method stub
Socket socket;
while(true){

try {
socket = serverSocket.accept();
new ReaderThread(socket).start();
new WriterThread(socket).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

class WriterThread extends Thread{
private Socket socket=null;
// BufferedReader inputReader=null;
BufferedWriter writer=null;
public WriterThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub

try {
System.out.println("开始输入");
writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// inputReader=new BufferedReader(new InputStreamReader(System.in));
new Timer().schedule(new TimerTask() {

@Override
public void run() {
// TODO Auto-generated method stub
try {
writer.write("老子在做测试\n");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 1000, 10000);
// String inputContent;
// while((inputContent=inputReader.readLine())!="byte"){
// System.out.println(inputContent);
//
// }

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ReaderThread extends Thread{
private Socket socket=null;
BufferedInputStream reader=null;
public ReaderThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub

try {

reader=new BufferedInputStream(socket.getInputStream());
System.out.println("接收到输入的信息");

int len=0;
byte[] bty=new byte[1024];
while ((len = reader.read(bty)) != -1) {
String inputContent=new String(bty,0,len);
System.out.println(inputContent);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
reader.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

 

客户端代码

package com.lotus.pushlangconnect;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

import javax.sound.sampled.AudioFormat.Encoding;

public class SocketClient {

public SocketClient() {

}
public static void main(String[] args) throws IOException {
SocketClient client=new SocketClient();
client.start();
}

public void start() throws IOException{
Socket socket=null;
try {
socket=new Socket("localhost", 9898);
new ReaderThread(socket).start();
new WriterThread(socket).start();

 

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
class WriterThread extends Thread{
private Socket socket=null;
BufferedReader inputReader=null;
BufferedWriter writer=null;
public WriterThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub

try {
System.out.println("开始输入");
writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
inputReader=new BufferedReader(new InputStreamReader(System.in));
String inputContent;
while((inputContent=inputReader.readLine())!="byte"){
// System.out.println(inputContent);
writer.write(inputContent);
writer.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
writer.close();
inputReader.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

class ReaderThread extends Thread{
private Socket socket=null;
BufferedReader reader=null;
public ReaderThread(Socket socket){
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub

try {

reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));

String respsonseContent;
while((respsonseContent=reader.readLine())!=null){
System.out.println(respsonseContent);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
reader.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

Socket多线程编程 serversocket服务器多线程编程

标签:

原文地址:http://www.cnblogs.com/lotus-emperor/p/4761386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!