Unable to connect to Sip server WebSocket transport error java.io.EOFException

I have created a SIP server using the JAIN-SIP library. I am using Apache Tomcat 9.0.95 with the Spring framework. When I can connect to the SIP server through WebSocket (ws://localhost:8080/society/ws) it gets connected, but when I try connecting it through jssip.min.js or sip.min.js, 

I receive the following error: Please help me to resolve this error.

`java.io.EOFException
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1343)
at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1231)
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:74)
at org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:184)
at org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:164)
at    org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:152)
at org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:60)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:57)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:937)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1791)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1190)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at java.base/java.lang.Thread.run(Thread.java:1570)`

WebSocket Configuration
Here is my WebSocket configuration:
I have set setAllowedOrigins as * and checked url

 `import org.springframework.context.annotation.Configuration;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 import org.springframework.web.servlet.config.annotation.CorsRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 import org.springframework.web.socket.config.annotation.EnableWebSocket;
 import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
 import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

  @Configuration
  @EnableWebSocket
  @EnableWebSecurity
   public class WebSocketConfig extends WebSecurityConfigurerAdapter implements  WebSocketConfigurer, WebMvcConfigurer {

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(new MyWebSocketHandler(), "/ws")
        .setAllowedOrigins("http://localhost:8080/society"); // Specify the allowed origin
}

@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/ws")
            .allowedOrigins("http://localhost:8080/society")
            .allowCredentials(true);
}

@Override
protected void configure(HttpSecurity http) {
    try {
        http
            .authorizeRequests()
            .antMatchers("/ws").permitAll() // Allow WebSocket connections
            .anyRequest().authenticated()
            .and()
            .csrf().disable(); // CSRF can interfere with WebSocket connections
    } catch (Exception e) {
        System.out.println("HttpSecurity error occurred");
        e.printStackTrace();
    }
   }
}`

WebSocket Handler
Below is my WebSocket handler:
Here I tried printing the payload also but it not getting printed.

 `import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import java.util.concurrent.ConcurrentHashMap;
  import javax.sip.*;
  import javax.sip.address.*;
  import javax.sip.header.*;
  import javax.sip.message.*;
  import javax.websocket.*;
  import org.springframework.web.socket.*;
  import org.springframework.web.socket.handler.TextWebSocketHandler;

  public class MyWebSocketHandler extends TextWebSocketHandler {
private SipProvider sipProvider;
private AddressFactory addressFactory;
private MessageFactory messageFactory;
private HeaderFactory headerFactory;
private Map<String, Session> webSocketSessions = new ConcurrentHashMap<>(); // Store WebSocket sessions per user

public MyWebSocketHandler() {
    try {
        SipServerConfig sipConfig = new SipServerConfig(); // Assuming your SipServerConfig is correctly set up
        this.sipProvider = sipConfig.sipProvider();
        this.addressFactory = sipConfig.addressFactory();
        this.messageFactory = sipConfig.messageFactory();
        this.headerFactory = sipConfig.headerFactory();
    } catch (Exception e) {
        System.out.println("MyWebSocketHandler error occurred");
        e.printStackTrace();
    }
}

@Override
public void handleTransportError(WebSocketSession session, Throwable exception) {
    System.out.println("WebSocket transport error with session: " + session.getId());
    exception.printStackTrace();
    try {
        session.close(CloseStatus.SERVER_ERROR);
    } catch (Exception e) {
        System.out.println("handleTransportError error occurred");
        e.printStackTrace();
    }
}

@Override
public void afterConnectionEstablished(WebSocketSession session) {
    System.out.println("WebSocket connection established with session: " + session.getId());
}

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
    System.out.println("WebSocket connection closed: " + session.getId() + ", status: " + status);
}

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
    try {
        String payload = message.getPayload();
        System.out.println("Received SIP message: " + payload);

        // Parse the SIP message
        Request request = createSipRequestFromMessage(payload);

        // Forward the SIP message to the SIP server via JAIN-SIP
        sipProvider.sendRequest(request);
    } catch (Exception e) {
        System.err.println("Error handling SIP message: " + e.getMessage());
        e.printStackTrace();
    }
}

private Request createSipRequestFromMessage(String payload) throws Exception {
    // This is where you'd parse the SIP message from the WebSocket payload
    // and create a SIP Request using the JAIN-SIP stack
    return null; // Implement the actual parsing logic
}

// Other methods omitted for brevity

}
`

Client-Side Code
Here is my client-side file:
I also tried using jssip.min.js and sip.min.js both are showing same error.
`

SIP Call

SIP Call Handling

Waiting for incoming call…

<button id="answerButton" style="display:none;" onclick="acceptCall()">Accept Call</button>
<button id="rejectButton" style="display:none;" onclick="rejectCall()">Reject Call</button>

<script>
    // SIP User Agent Configuration
    JsSIP.debug.enable('JsSIP:*'); // Enable full debug logging for JsSIP

    const socket = new JsSIP.WebSocketInterface('ws://localhost:8080/society/ws');
    const configuration = {
        sockets: [socket],
        uri: 'sip:user@localhost',
        contact_uri: 'sip:user@localhost:8080;transport=ws',
        trace_sip: true,
    };

    // Create a new SIP UserAgent
    const userAgent = new JsSIP.UA(configuration);

    // Start the UserAgent
    userAgent.start();

    // Register with the SIP server
    userAgent.register();
    </script>
  </body>
  </html>

`

What could be causing the java.io.EOFException error, and how can I resolve it?

New contributor

Komal Agarwal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật