重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
接收端(服务端)中的socket要实时处于监听状态,即要设置一个死循环。
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都做网站、外贸营销网站建设、富民网络推广、微信小程序、富民网络营销、富民企业策划、富民品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供富民建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
例如:
while(1){
//.....
}
当接收到一个客户端的消息,再为其开辟新的进程进行数据处理。
我也只是说个大概,具体建议网络上百度一下socket通信方法,看看人家服务器端是怎么写的。
你这根本什么都没写啊,原来写了个模拟QQ的,不过没去画界面,供你参考下,希望能帮到你,主要的是你把原理弄清楚就应该没问题了
服务器:
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.*;
public class Server
{
/**
* @param args
*/
private int count = 0;
private int num = 0;
HashMapInteger,Socket clients = new HashMapInteger,Socket();
public Server()
{
try
{
ServerSocket server = new ServerSocket(33333);
while(true)
{
Socket fromClient = server.accept();
count++;
num++;
clients.put(count, fromClient);
new Thread()
{
public void run()
{
receive();
}
}.start();
new Thread()
{
public void run()
{
send();
}
}.start();
}
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void receive()
{
try
{
BufferedReader read = new BufferedReader(new InputStreamReader(clients.get(count).getInputStream()));
int tmp = count;
int temp = 0;
while(true)
{
String value = "";
try
{
value = read.readLine();
String regex = "^ *~ *(\\d+) *:(.*)";
Pattern pat = Pattern.compile(regex);
Matcher match = pat.matcher(value);
if(match.find())
{
temp = Integer.valueOf(match.group(1));
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(clients.get(temp).getOutputStream()));
write.write("用户"+tmp+":"+match.group(2)+"\n");
write.flush();
}
else
{
for(int i = 1;i=num;i++)
{
if(i == tmp)
{
continue;
}
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(clients.get(i).getOutputStream()));
write.write("用户"+tmp+":"+value + "\n");
write.flush();
}
}
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch(IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void send()
{
Scanner scan = new Scanner(System.in);
String tmp = "";
while(!tmp.equals("exit"))
{
tmp = scan.nextLine();
try
{
for(int i = 1;i=num;i++)
{
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(clients.get(i).getOutputStream()));
write.write("系统:"+tmp + "\n");
write.flush();
}
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
new Server();
}
}
客户端:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client
{
/**
* @param args
*/
BufferedReader read;
BufferedWriter write;
public Client()
{
try
{
Socket client = new Socket("192.168.1.7",33333);
read = new BufferedReader(new InputStreamReader(client.getInputStream()));
write = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
new Thread()
{
public void run()
{
recieve();
}
}.start();
new Thread()
{
public void run()
{
send();
}
}.start();
//send();
}
catch(UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void send()
{
Scanner scan = new Scanner(System.in);
String tmp = "";
while(!tmp.equals("exit"))
{
tmp = scan.nextLine();
try
{
tmp= tmp+"\n";
write.write(tmp);
write.flush();
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
void recieve()
{
while(true)
{
try
{
String value = "";
value = read.readLine();
System.out.println(value);
write.flush();
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
new Client();
}
}
Server代码
package com.javacodegeeks.android.androidsocketserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
public class Server extends Activity
{
private ServerSocket serverSocket;
private Handler updateConversationHandler;
private Thread serverThread = null;
private TextView text;
public static final int SERVERPORT = 6000;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text2);
updateConversationHandler = new Handler();
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
}
@Override
protected void onStop()
{
super.onStop();
try
{
serverSocket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private class ServerThread implements Runnable
{
public void run()
{
Socket socket = null;
try
{
serverSocket = new ServerSocket(SERVERPORT);
}
catch (IOException e)
{
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted())
{
try
{
socket = serverSocket.accept(); // TODO
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();
}
catch (IOException e)
{
Log.i("liu", "socket.accept()失败");
e.printStackTrace();
}
}
}
}
private class CommunicationThread implements Runnable
{
private Socket clientSocket;
private BufferedReader input;
public CommunicationThread(Socket clientSocket)
{
this.clientSocket = clientSocket;
Log.i("liu", "获取到了client的Socket");
try
{
this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); // TODO
}
catch (IOException e)
{
Log.i("liu", "input获取失败");
e.printStackTrace();
}
}
public void run()
{
while (!Thread.currentThread().isInterrupted())
{
try
{
String read = input.readLine(); // TODO
Log.i("liu", read);
updateConversationHandler.post(new updateUIThread(read));
}
catch (IOException e)
{
Log.i("liu", "input读取失败");
e.printStackTrace();
}
}
}
}
private class updateUIThread implements Runnable
{
private String msg;
public updateUIThread(String str)
{
this.msg = str;
}
@Override
public void run()
{
text.setText(text.getText().toString() + "Client Says: " + msg + "\n");
}
}
}
2. Client代码:
package com.javacodegeeks.android.androidsocketclient;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class Client extends Activity
{
private Socket socket;
private static final int SERVERPORT = 4000;
private static final String SERVER_IP = "10.0.2.2";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new ClientThread()).start();
}
public void onClick(View view)
{
try
{
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
Log.i("liu", "点击按钮");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); // TODO
out.println(str);
}
catch (Exception e)
{
Log.i("liu", "write失败");
e.printStackTrace();
}
}
class ClientThread implements Runnable
{
@Override
public void run()
{
try
{
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
}
catch (UnknownHostException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
}