2013年11月18日星期一

than the problem of communication socket .

I as a server , A and B, both to connect me. Program is that whenever someone starts a thread connecting me to open a dialogue with this person interface , whenever he sent messages coming through the pipeline when the read information displayed on the screen . But tests found two people A and B strings screen, that is, A B send the information will go to the interface to go. They two threads how it will interfere with each other . . Seeking solutions.
------ Solution ---------------------------------------- ----

    
grace . But that is only useful for the latest launch .          
  
I do not know where the problem is , even if the old press the Send button and read the dialog box is the latest input .  

is not your dialog object references wrong
------ For reference only ------------------------ ---------------
send your program. Must be your code appears concurrent state inconsistency. A " thread closed" , or a " lock "
------ For reference only -------------------------- -------------
I think the landlord is not a programming problem . After the first connection is established , there will be a Socket object , you should first use the Socket connection at the same time , the user ID sent me , temporarily use A, B logo, then you should save a two -dimensional structure , it is best to HashMap this , numbered do Key, Socket doing Value, so you send a message by parsing the goals, such as , A sent B, sending number is A, the recipient number is B, you parse content, taken from the Map B corresponds in the socket object , and then to B hair. This order fishes . You are not confused into mass news ?
------ For reference only -------------------------------------- -
[/ code] Since the source code of several files I do not fully made ​​, only made the relevant section.
open ports:
while(true)
{
  ServerSocket server_socket=new ServerSocket(i1); //i1是端口号
  server threadServer=new server(server_socket.accept());
  threadServer.start();
}

server class :
import java.io.*;
import java.net.*; 
public class server extends Thread
{
int chatcreat;
    private Socket myClient;
    public server(Socket c)
    {
        this.myClient=c;
        chatcreat=0;
    }

    public void run()
    {
        try
        { 
            BufferedReader in=new BufferedReader(new InputStreamReader(myClient.getInputStream()));
            PrintWriter out=new PrintWriter(myClient.getOutputStream());
            System.out.println("Wait for client to send string...");
            //System.out.println("chatcreat="+chatcreat);
            String name=in.readLine();
            if((!name.equals(""))&&chatcreat==0)    
            {
             chatcreat++;
             new chatinter(name,myClient).start();   //在客户端发给服务器的第一条消息前先把客户端自定义的昵称发到服务器上。chatinter是对话界面
            }
        }
        catch(IOException ex){}
        finally{}
    }
}


interface displays the message part:
		try
{
BufferedReader in=new BufferedReader(new InputStreamReader(myClient.getInputStream()));
out=new PrintWriter(myClient.getOutputStream());
while(true)
{
String str=in.readLine();
a1.append(tit+":"+str+"\n");//a1是JTextArea,tit是前面传过来的客户端的昵称
}
}
catch (Exception e1)
{
System.out.println(e1);
}

------ For reference only ----------------------------------- ----
still can not figure out why. . Do not sink ah
------ For reference only ----------------------------------- ----
which Socket input, processing, then sent out the same Socket ouput



 ServerSocket server_socket=new ServerSocket(i1);    //i1是端口号       

while(true)
{
  //放在此处会创建多个ServerSocket 
  server threadServer=new server(server_socket.accept());
  threadServer.start();       
}


unreasonable strings
------ For reference only ------------------------------ ---------


add newly discovered . After the first two 1st person personal connection interface on the failure , not a message , press the button does not respond , the message will not be updated. I'll give the first person to send a message is also displayed in the interface of the second person .
------ For reference only -------------------------------------- -

  
add newly discovered . After the first two 1st person personal connection interface on the failure , not a message , press the button does not respond , the message will not be updated. I'll give the first person to send a message is also displayed in the interface of the second person .  

The reason is : the code each time before you create a new ServerSocket object , therefore, only the latest Socket effective access
------ For reference only ----- ----------------------------------
upstairs positive solution, as long as the first one in while truee while , then moved to the outside like a
------ For reference only ------------------------------ ---------

    
add newly discovered . After the first two 1st person personal connection interface on the failure , not a message , press the button does not respond , the message will not be updated. I'll give the first person to send a message is also displayed in the interface of the second person .          
  
The reason is : the code each time before you create a new ServerSocket object , therefore, only the latest Socket effective access  

Then how can I solve it .





do so the first two individual interface will not come out . .
------ For reference only -------------------------------------- -
  The reply deleted by an administrator at 2013-11-16 16:19:05

------ For reference only ---------------------------------- -----
if the server class definition into the class declaration , the two people connect , the server to the client a message in the text field , then enter the information could not be read , press the " send" button to send the past is the empty message is displayed in the client touches inside an interface . Client 1 , then the server will display a message to the server to the client 2 interface , the content is normal. Client 2 has no effect
------ For reference only --------------------------------- ------

      
add newly discovered . After the first two 1st person personal connection interface on the failure , not a message , press the button does not respond , the message will not be updated. I'll give the first person to send a message is also displayed in the interface of the second person .                
    
The reason is : the code each time before you create a new ServerSocket object , therefore, only the latest Socket effective access          
  
Then how can I solve it .   
  
  
  
     
  
do so the first two individual interface will not come out . .  

interface and ServerSocket is not related !
------ For reference only -------------------------------------- -

        
add newly discovered . After the first two 1st person personal connection interface on the failure , not a message , press the button does not respond , the message will not be updated. I'll give the first person to send a message is also displayed in the interface of the second person .                      
      
The reason is : the code each time before you create a new ServerSocket object , therefore, only the latest Socket effective access                
    
Then how can I solve it .     
    
    
    
         
    
do so the first two individual interface will not come out . .          
  
interface and ServerSocket is not related !  

interface is connected to receive the first two people will come out ah.
server threadServer = new server (server_socket.accept ()); sentence do not get out while receiving a client can only Mody
------ For reference only - -------------------------------------


landlord to see me examples Click to download page , I have done this and saved user names and a list of yes yes corresponding connection , so very simple
------ For reference only ------------------------------ ---------

  
landlord to see me examples    Click to download page , I have done this and saved user names and a list of yes yes corresponding connection , so very simple  

Thank you . But I was trying to figure out my main problem lies in what this program . . I made ​​a paste to another program Quanfa Come on
------ For reference only -------------------------- -------------

          
add newly discovered . After the first two 1st person personal connection interface on the failure , not a message , press the button does not respond , the message will not be updated. I'll give the first person to send a message is also displayed in the interface of the second person .                            
        
The reason is : the code each time before you create a new ServerSocket object , therefore, only the latest Socket effective access                      
      
Then how can I solve it .       
      
      
      
             
      
do so the first two individual interface will not come out . .                
    
interface and ServerSocket is not related !          
  
interface is connected to receive the first two people will come out ah.   
server threadServer = new server (server_socket.accept ()); sentence do not get out while receiving a client can only Mody  

First of all:

get out the ServerSocket server_socket = new ServerSocket (i1); / / i1 is the port number
rather than accept

Are you sure ?

ServerSocket to listen for client connection requests generate a Socket

no direct correlation between different Scoket
------ For reference only --------------------------- ------------


amount . I've changed like this:

public void run()
{
try
{
i1=Integer.parseInt(t1.getText());
name=t2.getText();
if(name.equals(""))
System.out.println("请输入你的昵称!");
else
{
server_socket=new ServerSocket(i1);
indexset.connectsucceed();
//server threadServer;
while(true)
{
//System.out.println("Wait for client to connect...");
Socket a=server_socket.accept();
//server threadServer=new server(server_socket.accept());
server threadServer=new server(a);
threadServer.start();
}
}
}
catch(Exception e1){ System.out.println("ServerSocket启动失败!");};
}

but still not resolved . . I put all the procedures are uploaded http://download.csdn.net/download/ww01055/6569887
- ----- For reference only ---------------------------------------
you are connected to each client initiates a chat box ?
------ For reference only -------------------------------------- -


grace . But that is only useful for the latest launch .
------ For reference only -------------------------------------- -

  
grace . But that is only useful for the latest launch .  

I do not know where the problem is , even if the old press the Send button and read the dialog box is the latest input .
------ For reference only -------------------------------------- -
[quote = 21st floor victor_woo reply quote :
is not your mistake dialog object references

egg pain . Really are. Are you such a reminder found written in the text field of the static type . . . No wonder . Hard upstairs it. Alas

没有评论:

发表评论