Raised This Month: $51 Target: $400
 12% 

Solved Socket Problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
tpr
Junior Member
Join Date: Aug 2014
Location: where my heart is
Old 01-21-2022 , 15:13   Socket Problem
Reply With Quote #1

hi,

im trying to send a string via socket.
server is written in java and client is in sourcemod.


sourcemod part :
Code:

#include <sourcemod>
#include <socket>

#pragma semicolon 1
#pragma tabsize 0

Handle socket;
public Plugin myinfo =
{
	name = "socket test",
	author = "",
	description = "",
	version = "1",
	url = ""
};		

public void OnPluginStart(){


	RegAdminCmd("testsock" , send_pm , ADMFLAG_BAN);

	socket = SocketCreate(SOCKET_TCP, OnSocketError);
	SocketConnect(socket, OnSocketConnected, OnSocketReceive, OnSocketDisconnected, "localhost", 9876);

	
}


public Action send_pm(int client , int args){


	char requestStr[100];
	Format(requestStr, sizeof(requestStr), "hello from sourcemod\n");

	SocketSend(socket, requestStr);

	// connect the socket
	


    return Plugin_Continue;
}

public OnSocketConnected(Handle sockett, any arg) {
	// socket is connected, send the http request
	PrintToServer("socket connected");
}

public OnSocketReceive(Handle sockett, char[] receiveData, const int dataSize, any hFile) {
	// receive another chunk and write it to <modfolder>/dl.htm
	// we could strip the http response header here, but for example's sake we'll leave it in

	//WriteFileString(hFile, receiveData, false);
}

public OnSocketDisconnected(Handle sockett, any hFile) {
	// Connection: close advises the webserver to close the connection when the transfer is finished
	// we're done here

	CloseHandle(hFile);
	CloseHandle(sockett);
}

public OnSocketError(Handle sockett, const int errorType, const int errorNum, any hFile) {
	// a socket error occured

	LogError("socket error %d (errno %d)", errorType, errorNum);
	CloseHandle(hFile);
	CloseHandle(sockett);
}

java part :

Code:
private static int port = 9876;
           
    try{
            ServerSocket server = new ServerSocket(port); 
      
        
        while(true){
            System.out.println("Waiting for sourcemod");
          
            Socket socket = server.accept();
         
  //  InputStream in = socket.getInputStream();
                 //   out.println(new Date().toString());
            DataInputStream dis = new DataInputStream(socket.getInputStream());  
            String  str=(String)dis.readUTF();  
            System.out.println("message= "+ str);  

            

             
      
                if(str.toString().equals("exit")){
                
                socket.close();
                System.out.println("Shutting down Socket server!!");
                break;
                
                }
        
             }

}
    catch(Exception e){
    
    
    }

java part is ok since i made a client with java and its working.

but , no message will be shown in server console when i try to send msg via sourcemod.
it only said that socket is connected ( in game server console ).

and when i close the game server , it will cause server to face exception ( connection refused )


could anyone help me pls ?
__________________
Mireda

Last edited by tpr; 01-21-2022 at 16:46. Reason: solved
tpr is offline
tpr
Junior Member
Join Date: Aug 2014
Location: where my heart is
Old 01-21-2022 , 16:47   Re: Socket Problem
Reply With Quote #2

i solved this by reading the output by bytes

here is the code for who has this problem :

InputStream is = socket.getInputStream();

byte[] buffer = new byte[1024];
int read;
while((read = is.read(buffer)) != -1) {
String output = new String(buffer, 0, read);
System.out.print(output);
System.out.flush();
};
__________________
Mireda
tpr is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:10.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode