当前位置: 技术问答>java相关
如何运行socket的程序呀?
来源: 互联网 发布时间:2015-08-01
本文导语: 小弟第一次接触socket,我还不会运行呢,朋友们帮忙呀。 追豪举个例子,谢谢。 | 服务器端不要什么配置的,只要客户端设置与服务器一致就行例如:add和port分别代表服务器的IP地址和端口...
小弟第一次接触socket,我还不会运行呢,朋友们帮忙呀。
追豪举个例子,谢谢。
追豪举个例子,谢谢。
|
服务器端不要什么配置的,只要客户端设置与服务器一致就行例如:add和port分别代表服务器的IP地址和端口号,那么客户端
Socket socket=new Socket(add,port);
就ok了
至于运行,你可以建两个工程,一个做服务器,一个做客户端,要先运行服务器;或者在两台机器上作,一个做服务器,一个做客户端
Socket socket=new Socket(add,port);
就ok了
至于运行,你可以建两个工程,一个做服务器,一个做客户端,要先运行服务器;或者在两台机器上作,一个做服务器,一个做客户端
|
服务器端不用设置的,只要设置好客户端就行了,例如服务器的地址为add,端口号为port,那么客户端只要:
Socket socket=new Socket(add,port);就ok了。
运行它们可以建开两个应用,一个作客户端,一个作服务器端,先运行服务器端;或者在连台机器上分别运行就行了。
Socket socket=new Socket(add,port);就ok了。
运行它们可以建开两个应用,一个作客户端,一个作服务器端,先运行服务器端;或者在连台机器上分别运行就行了。
|
import java.io.*;
import java.net.*;
import java.util.Vector;
/**
*
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class TestSocketServer
{
ServerSocket mServerSocket = null;
Socket mSocket = null;
public Vector mAllClientVector = new Vector(5,2);//存放所有客户端信息
public TestSocketServer()
{
try
{
mServerSocket = new ServerSocket(9009,15);
mSocket = mServerSocket.accept();
ReceiveClientThread rCT = new ReceiveClientThread(this,mSocket);
rCT.start();
}
catch(Exception eSocket)
{
}
}
public static void main(String [] args)
{
new TestSocketServer();
}
}
class ReceiveClientThread extends Thread
{
private TestSocketServer mTestSocketServer = null;
private Socket mSocket = null;
private boolean mRunning = true;
DataInputStream dis = null;
DataOutputStream dos = null;
Vector mVector = null;
public ReceiveClientThread(TestSocketServer pTestSocketServer,Socket pSocket)
{
this.mTestSocketServer = pTestSocketServer;
this.mSocket = pSocket;
}
public void run()
{
while(true)
{
if(!mRunning)
{
//在内存中删除相应的客户端信息
break;
}
try
{
dis = new DataInputStream(mSocket.getInputStream());
ObjectInputStream ois = new ObjectInputStream(dis);
mVector = (Vector)ois.readObject();
for(int i = 0 ; i
import java.net.*;
import java.util.Vector;
/**
*
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
public class TestSocketServer
{
ServerSocket mServerSocket = null;
Socket mSocket = null;
public Vector mAllClientVector = new Vector(5,2);//存放所有客户端信息
public TestSocketServer()
{
try
{
mServerSocket = new ServerSocket(9009,15);
mSocket = mServerSocket.accept();
ReceiveClientThread rCT = new ReceiveClientThread(this,mSocket);
rCT.start();
}
catch(Exception eSocket)
{
}
}
public static void main(String [] args)
{
new TestSocketServer();
}
}
class ReceiveClientThread extends Thread
{
private TestSocketServer mTestSocketServer = null;
private Socket mSocket = null;
private boolean mRunning = true;
DataInputStream dis = null;
DataOutputStream dos = null;
Vector mVector = null;
public ReceiveClientThread(TestSocketServer pTestSocketServer,Socket pSocket)
{
this.mTestSocketServer = pTestSocketServer;
this.mSocket = pSocket;
}
public void run()
{
while(true)
{
if(!mRunning)
{
//在内存中删除相应的客户端信息
break;
}
try
{
dis = new DataInputStream(mSocket.getInputStream());
ObjectInputStream ois = new ObjectInputStream(dis);
mVector = (Vector)ois.readObject();
for(int i = 0 ; i